Tkinter library to create a graphical user interface (GUI) in Python that displays information about a car:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
import tkinter as tk class Car: def __init__(self, make, model, year): self.make = make self.model = model self.year = year def __str__(self): return f"{self.year} {self.make} {self.model}" def display_car_info(): car = Car("Toyota", "Camry", 2020) car_info.config(text=str(car)) root = tk.Tk() root.title("Car Information") car_info = tk.Label(root, text="") car_info.pack() show_info_button = tk.Button(root, text="Show Car Info", command=display_car_info) show_info_button.pack() root.mainloop() |
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:
Widget | Description | Example Syntax |
---|---|---|
Button | A button that can be clicked to perform an action | button = tk.Button(root, text="Click Me!", command=callback_function) |
Label | A widget used to display text | label = tk.Label(root, text="Hello, World!") |
Entry | A single-line text entry widget | entry = tk.Entry(root) |
Text | A multi-line text entry widget | text = tk.Text(root) |
Checkbutton | A checkbox that can be checked or unchecked | checkbutton = tk.Checkbutton(root, text="Option", variable=var) |
Radiobutton | A button that can be selected or deselected in a group of mutually exclusive buttons | radiobutton = tk.Radiobutton(root, text="Option", variable=var, value=1) |
Listbox | A list of selectable items | listbox = tk.Listbox(root) |
Menu | A menu that can be attached to a window or a menu item | menu = tk.Menu(root) |
Scrollbar | A widget used to add scrolling to a widget | scrollbar = 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:
Widget | Description | Example Syntax |
---|---|---|
Canvas | A widget used to draw shapes and images | canvas = tk.Canvas(root, width=200, height=100) |
Frame | A container widget that can be used to group other widgets | frame = tk.Frame(root) |
Scale | A slider that can be used to select a numerical value | scale = tk.Scale(root, from_=0, to=100, orient="horizontal") |
Spinbox | A widget that allows the user to select a value by spinning a dial | spinbox = tk.Spinbox(root, from_=0, to=100) |
Message | A widget used to display multiline text that can wrap to the size of the widget | message = tk.Message(root, text="Hello, World!") |
Toplevel | A window that can act as the parent of other widgets | toplevel = tk.Toplevel(root) |
OptionMenu | A menu that displays a drop-down list of options | option_menu = tk.OptionMenu(root, var, *options) |
Progressbar | A widget used to display the progress of a task | progressbar = 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.