In this lesson we want to learn about TKinter Applications with simple dialogs and pop up windows, Tkinter is standard GUI (Graphical User Interface) library for Python. It provides set of tools for creating graphical user interfaces that are portable across multiple platforms, including Windows, macOS and Linux. Tkinter provides simple and intuitive way to create windows, dialogs, buttons, labels and other GUI elements using Python. with Tkinter you can create different types applications from simple desktop applications to more complex ones, such as data visualization tools, games and multimedia players. Tkinter is included with most Python installations and is easy to learn and use, making it a popular choice for many Python developers.
TKinter Applications with simple dialogs and pop-up windows
In Tkinter, you can create simple dialogs and pop up windows using tkinter.simpledialog module. simpledialog module provides several classes for creating dialogs including:
- askinteger: Prompts the user to enter an integer.
- askfloat: Prompts the user to enter a floating-point number.
- askstring: Prompts the user to enter a string.
- askquestion: Prompts the user with a yes/no question.
this is an example of how to use the askstring class to create simple dialog:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
import tkinter as tk import tkinter.simpledialog def show_dialog(): result = tkinter.simpledialog.askstring("Input", "Enter your name:") label.config(text="Hello, " + result) root = tk.Tk() root.title("Main Window") root.geometry("200x100") label = tk.Label(root, text="") label.pack() button = tk.Button(root, text="Show Dialog", command=show_dialog) button.pack() root.mainloop() |
In this example, the show_dialog function creates an askstring dialog when the Show Dialog button is clicked. dialog prompts the user to enter a string, and the result is displayed in label. Note that you can use the other simpledialog classes in a similar way to create different types of dialogs.
Run the complete code and this will be the result.