📅  最后修改于: 2020-10-25 03:48:48             🧑  作者: Mango
Tkinter教程提供了Python Tkinter的基本和高级概念。我们的Tkinter教程专为初学者和专业人士设计。
Python提供了标准库Tkinter来为基于桌面的应用程序创建图形用户界面。
使用Python Tkinter开发基于桌面的应用程序并不是一项复杂的任务。可以使用以下步骤创建一个空的Tkinter顶层窗口。
# !/usr/bin/python3
from tkinter import *
#creating the application main window.
top = Tk()
#Entering the event main loop
top.mainloop()
输出:
有各种小部件,例如按钮,画布,复选按钮,条目等,用于构建Python GUI应用程序。
SN | Widget | Description |
---|---|---|
1 | Button | The Button is used to add various kinds of buttons to the python application. |
2 | Canvas | The canvas widget is used to draw the canvas on the window. |
3 | Checkbutton | The Checkbutton is used to display the CheckButton on the window. |
4 | Entry | The entry widget is used to display the single-line text field to the user. It is commonly used to accept user values. |
5 | Frame | It can be defined as a container to which, another widget can be added and organized. |
6 | Label | A label is a text used to display some message or information about the other widgets. |
7 | ListBox | The ListBox widget is used to display a list of options to the user. |
8 | Menubutton | The Menubutton is used to display the menu items to the user. |
9 | Menu | It is used to add menu items to the user. |
10 | Message | The Message widget is used to display the message-box to the user. |
11 | Radiobutton | The Radiobutton is different from a checkbutton. Here, the user is provided with various options and the user can select only one option among them. |
12 | Scale | It is used to provide the slider to the user. |
13 | Scrollbar | It provides the scrollbar to the user so that the user can scroll the window up and down. |
14 | Text | It is different from Entry because it provides a multi-line text field to the user so that the user can write the text and edit the text inside it. |
14 | Toplevel | It is used to create a separate window container. |
15 | Spinbox | It is an entry widget used to select from options of values. |
16 | PanedWindow | It is like a container widget that contains horizontal or vertical panes. |
17 | LabelFrame | A LabelFrame is a container widget that acts as the container |
18 | MessageBox | This module is used to display the message-box in the desktop based applications. |
Tkinter几何体指定了在显示屏上表示小部件所使用的方法。 Python Tkinter提供以下几何方法。
让我们详细讨论其中的每一个。
pack()小部件用于组织块中的小部件。使用pack()方法添加到Python应用程序中的positions小部件可以通过使用方法调用中指定的各种选项来控制。
但是,控件较少,并且通常以较少组织的方式添加了小部件。
下面给出了使用pack()的语法。
widget.pack(options)
下面给出了可以在pack()中传递的可能选项的列表。
# !/usr/bin/python3
from tkinter import *
parent = Tk()
redbutton = Button(parent, text = "Red", fg = "red")
redbutton.pack( side = LEFT)
greenbutton = Button(parent, text = "Black", fg = "black")
greenbutton.pack( side = RIGHT )
bluebutton = Button(parent, text = "Blue", fg = "blue")
bluebutton.pack( side = TOP )
blackbutton = Button(parent, text = "Green", fg = "red")
blackbutton.pack( side = BOTTOM)
parent.mainloop()
输出:
grid()几何管理器以表格形式组织小部件。我们可以将行和列指定为方法调用中的选项。我们还可以指定窗口小部件的列跨度(宽度)或行跨度(高度)。
这是将窗口小部件放置到Python应用程序的一种更有条理的方式。下面给出了使用grid()的语法。
widget.grid(options)
下面给出了可以在grid()方法内部传递的可能选项的列表。
# !/usr/bin/python3
from tkinter import *
parent = Tk()
name = Label(parent,text = "Name").grid(row = 0, column = 0)
e1 = Entry(parent).grid(row = 0, column = 1)
password = Label(parent,text = "Password").grid(row = 1, column = 0)
e2 = Entry(parent).grid(row = 1, column = 1)
submit = Button(parent, text = "Submit").grid(row = 4, column = 0)
parent.mainloop()
输出:
place()几何管理器将小部件组织到特定的x和y坐标。
widget.place(options)
下面列出了可能的选项。
# !/usr/bin/python3
from tkinter import *
top = Tk()
top.geometry("400x250")
name = Label(top, text = "Name").place(x = 30,y = 50)
email = Label(top, text = "Email").place(x = 30, y = 90)
password = Label(top, text = "Password").place(x = 30, y = 130)
e1 = Entry(top).place(x = 80, y = 50)
e2 = Entry(top).place(x = 80, y = 90)
e3 = Entry(top).place(x = 95, y = 130)
top.mainloop()
输出:
在学习Tkinter之前,您必须具有Python的基本知识。
我们的Python Tkinter教程旨在帮助初学者和专业人士。
我们保证您不会在本Tkinter教程中找到任何问题。但是,如果有任何错误,请在联系表格中发布问题。