- 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.
推薦閱讀
- UI圖標創意設計
- Learning NServiceBus(Second Edition)
- Linux操作系統基礎案例教程
- 自制編程語言
- Node Cookbook(Second Edition)
- SQL Server實用教程(SQL Server 2008版)
- 持續輕量級Java EE開發:編寫可測試的代碼
- Machine Learning With Go
- Java Web開發實例大全(基礎卷) (軟件工程師開發大系)
- Learning Nessus for Penetration Testing
- After Effects CC技術大全
- PostgreSQL Developer's Guide
- 秒懂算法:用常識解讀數據結構與算法
- 交互設計語言:與萬物對話的藝術(全兩冊)
- Learning RxJava