- Tkinter GUI Application Development Blueprints(Second Edition)
- Bhaskar Chaudhary
- 279字
- 2021-06-24 18:35:02
Widgets – the building blocks of GUI programs
Now that we have our top level or the root window ready, it is time to think over the question: which components should appear in the window? In Tkinter jargon, these components are called widgets.
The syntax that is used to add a widget is as follows:
my_widget = tk.Widget-name (its container window, ** its configuration options)
In the following example ( 1.02.py ), we will add two widgets, a label and a button, to the root container. Also, note how all the widgets are added between the skeleton code that we defined in the first example:
import tkinter as tk
root = tk.Tk()
label = tk.Label(root, text="I am a label widget")
button = tk.Button(root, text="I am a button")
label.pack()
button.pack()
root.mainloop()
Running the preceding code(1.02.py) will generate a window with a label and a button widget, as shown in the following screenshot:

The following is a description of the preceding code:
- This code added a new instance named label for the label widget. The first parameter defined root as its parent or container. The second parameter configured its text option to read I am a label widget.
- Similarly, we defined an instance of a Button widget. This is also bound to the root window as its parent.
- We used the pack() method, which is essentially required to position the label and button widgets within the window. We will discuss the pack() method and several other related concepts when exploring the geometry management task. However, you must note that some sort of geometry specification is essential for the widgets to be displayed.
推薦閱讀
- Redis Applied Design Patterns
- Mastering Articulate Storyline
- Python計算機視覺編程
- Mastering matplotlib
- Learning Apache Karaf
- CoffeeScript Application Development Cookbook
- Android移動開發案例教程:基于Android Studio開發環境
- MINECRAFT編程:使用Python語言玩轉我的世界
- OpenCV 3計算機視覺:Python語言實現(原書第2版)
- C指針原理揭秘:基于底層實現機制
- WCF技術剖析(卷1)
- 網頁設計與制作
- Python滲透測試編程技術:方法與實踐(第2版)
- Puppet Cookbook(Third Edition)
- Practical Linux Security Cookbook