GUI (Graphical User Interface):
GUI(圖形用戶界面):
GUI is a simple application which helps the user to interact with the computer or any other electronic device through a graphical icon. This used to perform different tasks on a desktop or laptop.
GUI是一個簡單的應用程序,可以幫助用戶通過圖形圖標與計算機或任何其他電子設備進行交互。 這通常用于在臺式機或筆記本電腦上執行不同的任務。
GUI tkinter模塊 (GUI tkinter module)
tkinter is an inbuilt Python module used to create a GUI application. Python offers a lot of options for creating GUI out of which tkinter is most commonly used. You don't need to worry about installation because it comes with Python.
tkinter是用于創建GUI應用程序的內置Python模塊。 Python提供了許多創建tkinter的 GUI的選項。 您無需擔心安裝,因為它是Python附帶的。
There is the most common way to create a GUI application using tkinter:
有使用tkinter創建GUI應用程序的最常用方法:
Import the tkinter module in the program.
將tkinter模塊導入程序中。
Create the GUI application's main window.
創建GUI應用程序的主窗口 。
Add any number of widgets to the GUI application's main window.
將任意數量的小部件添加到GUI應用程序的主窗口。
Apply the main event loop to widgets.
將主事件循環應用于小部件 。
There are mainly two methods we have to remember during the creation of the GUI application by using tkinter module in Python.
在使用Python中的tkinter模塊創建GUI應用程序的過程中,我們主要需要記住兩種方法。
1) tkinter.Tk()
1)tkinter.Tk()
To create the main window of the GUI application tkinter offers a Tk() function.
為了創建GUI應用程序的主窗口,tkinter提供了Tk()函數。
Syntax:
句法:
Includehelp=tkinter.Tk()
Where, Includehelp is the name of the GUI application's main window.
其中, Includehelp是GUI應用程序主窗口的名稱。
2) mainloop()
2)mainloop()
This is used when you are ready for the application to run. This telling the code keep displaying the window until manually closed it.
當您準備好運行應用程序時,將使用它。 這告訴代碼繼續顯示該窗口,直到手動將其關閉為止。
Syntax:
句法:
Includehelp.mainloop()
In tkinter, all widgets will have some geometry measurement and it has three mainly geometry manager classes which are discussed below.
在tkinter中 ,所有小部件都將具有一些幾何尺寸,并且它具有以下三個主要的幾何管理器類。
pack(): It organizes the widgets in blocks before placing them in the parent widget.
pack() :在將小部件放置在父小部件中之前,將它們按塊進行組織。
grid(): It organizes the widgets in the grid before placing it in the parent widget.
grid() :在將其放置在父窗口小部件中之前,它會組織網格中的窗口小部件。
place(): It organizes the widgets by placing them on specific positions directed by us.
place() :通過將小部件放在我們指示的特定位置來組織小部件。
In tkinter, there is a lot of widgets provided to use in the GUI application. Some of these major widgets will be discussed below:
在tkinter中 ,提供了許多可在GUI應用程序中使用的小部件。 其中一些主要小部件將在下面討論:
1) Button
1)按鈕
To add a button in the GUI application, we are using this widget in the program.
要在GUI應用程序中添加按鈕,我們在程序中使用此小部件。
Syntax:
句法:
button_name=tkinter,Button(
parents_window_name,
text='text_writing',
width='width_of_text',
command='function to call')
2) Radio Button
2)單選按鈕
The widgets used to offer multiple options to the user. To add radio button we can simply use RadioButton class.
這些小部件用于向用戶提供多個選項。 要添加單選按鈕,我們可以簡單地使用RadioButton類。
rad1=Radiobutton(
Parents_window_name,
text='text_writing',
value=numerical_value).pack()
We have to give a different value for every radio button, otherwise, they won't work.
我們必須為每個單選按鈕指定不同的值,否則它們將無法工作。
3) Listbox
3)列表框
This widget offers a list of options to the user to select any options.
該小部件向用戶提供選項列表,以選擇任何選項。
Syntax:
句法:
List=Listbox(Parents_window_name)
List.insert(1, text)
List.insert(2, nexttext)
4) Entry
4)進入
This widget provide user to enter multiple line text input.
該小部件提供用戶輸入多行文本輸入的功能。
Syntax:
句法:
label1=Label(Parents_window_name, text='Name').grid(row=0)
label2=Label(Parents_window_name, text='password').grid(row=1)
entry_in_label1 = Entry(Parents_window_name)
entry_in_label2 = Entry(Parents_window_name)
entry_in_label1.grid(row=0, column=1)
entry_in_label2.grid(row=1, column=1)
The implementation of these methods is in the below program.
這些程序的實現在下面的程序中。
Program:
程序:
import tkinter
from tkinter import *
Includehelp.title('Includehelp')
rad1=Radiobutton(Includehelp, text='Content', value=1).pack()
rad2=Radiobutton(Includehelp, text='Software', value=2).pack()
lebel=tkinter.Button(Includehelp,text='Welcome to Includehelp', width=100).pack()
Includehelp.mainloop()
翻譯自: https://www.includehelp.com/python/gui-programming-in-python-using-tkinter-module.aspx