Python: How to put a border around an OptionMenu using Tkinter?


To save the contents of a Textbox in Tkinter, we can take the following steps −

  • Create an instance of tkinter frame.

  • Set the size of the frame using win.geometry method.

  • Define a user-defined method "open_text" to open a text file in "read" mode. Read the contents of the text file and save it in a variable called "content". Then, use the "insert" method to insert the content in a Textbox.

  • Next, define another user-defined method called "save_text" and in it, use the "write" method to save the contents of the textbox in the text file.

  • Create a text widget using the Text method with specified height and width.

  • Create a button to call the open_text method.

  • Finally, run the mainloop of the application window.

Example

Take a look at the following example −

# Import Tkinter library
from tkinter import *

# Create an instance of Tkinter frame or window
win = Tk()

# Set the geometry of tkinter frame
win.geometry("716x300")

# Create Menu Items
options=("Cellphone", "Laptop", "Smartwatch", "Digital Camera")

# Find the length of maximum character in the option
menu_width = len(max(options, key=len))

# Create an OptionMenu
menu = OptionMenu(win, options[0], *options)
menu.config(width=menu_width, borderwidth=5, activebackground="green")

menu.pack(pady=30, ipadx=10)

win.mainloop()

Output

On execution, it will produce the following output −

Updated on: 05-Sep-2023

188 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements