Python GUI (Tkinter)

Tkinter library to create a graphical user interface (GUI) in Python that displays information about a car:

This code defines a Car class with make, model, and year attributes, and a __str__ method that returns a string representation of the car. It also creates a GUI with a button that, when clicked, calls the display_car_info function which creates an instance of the Car class and sets the text of a Label widget to the string representation of the car. When you run the code, a window will open that displays the button, and when you click the button, the information about the car will be displayed.

Below is table that lists some of the most commonly used widgets in Tkinter with a brief description and example syntax:

WidgetDescriptionExample Syntax
ButtonA button that can be clicked to perform an actionbutton = tk.Button(root, text="Click Me!", command=callback_function)
LabelA widget used to display textlabel = tk.Label(root, text="Hello, World!")
EntryA single-line text entry widgetentry = tk.Entry(root)
TextA multi-line text entry widgettext = tk.Text(root)
CheckbuttonA checkbox that can be checked or uncheckedcheckbutton = tk.Checkbutton(root, text="Option", variable=var)
RadiobuttonA button that can be selected or deselected in a group of mutually exclusive buttonsradiobutton = tk.Radiobutton(root, text="Option", variable=var, value=1)
ListboxA list of selectable itemslistbox = tk.Listbox(root)
MenuA menu that can be attached to a window or a menu itemmenu = tk.Menu(root)
ScrollbarA widget used to add scrolling to a widgetscrollbar = tk.Scrollbar(root, orient="vertical", command=text.yview)

Note: root in the example syntax refers to the root window of the Tkinter GUI.

some additional Tkinter widgets:

WidgetDescriptionExample Syntax
CanvasA widget used to draw shapes and imagescanvas = tk.Canvas(root, width=200, height=100)
FrameA container widget that can be used to group other widgetsframe = tk.Frame(root)
ScaleA slider that can be used to select a numerical valuescale = tk.Scale(root, from_=0, to=100, orient="horizontal")
SpinboxA widget that allows the user to select a value by spinning a dialspinbox = tk.Spinbox(root, from_=0, to=100)
MessageA widget used to display multiline text that can wrap to the size of the widgetmessage = tk.Message(root, text="Hello, World!")
ToplevelA window that can act as the parent of other widgetstoplevel = tk.Toplevel(root)
OptionMenuA menu that displays a drop-down list of optionsoption_menu = tk.OptionMenu(root, var, *options)
ProgressbarA widget used to display the progress of a taskprogressbar = tk.Progressbar(root, orient="horizontal", length=200, mode="determinate")

Note: var in the OptionMenu example syntax refers to a Tkinter variable, and options is a list of options to be displayed in the drop-down list.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top