📅  最后修改于: 2020-12-23 05:29:11             🧑  作者: Mango
Python提供了多种开发图形用户界面(GUI)的选项。最重要的列出如下。
Tkinter的– Tkinter的是Python接口附带的Python Tk的GUI工具包。我们将在本章中查找此选项。
wxPython-这是wxWindows http://wxpython.org的开源Python接口。
JPython -JPython是Java的Python端口,可让Python脚本无缝访问本地计算机http://www.jython.org上的Java类库。
还有许多其他可用接口,您可以在网上找到它们。
Tkinter是Python的标准GUI库。Python与Tkinter结合使用时,可以轻松快速地创建GUI应用程序。 Tkinter为Tk GUI工具包提供了强大的面向对象的界面。
使用Tkinter创建GUI应用程序很容易。您需要做的就是执行以下步骤-
导入Tkinter模块。
创建GUI应用程序主窗口。
将一个或多个上述小部件添加到GUI应用程序。
进入主事件循环以对用户触发的每个事件采取措施。
#!/usr/bin/python
import Tkinter
top = Tkinter.Tk()
# Code to add widgets will go here...
top.mainloop()
这将创建以下窗口-
Tkinter提供了各种控件,例如GUI应用程序中使用的按钮,标签和文本框。这些控件通常称为小部件。
Tkinter当前有15种类型的小部件。下表中列出了这些小部件以及简要说明-
Sr.No. | Operator & Description |
---|---|
1 | Button
The Button widget is used to display buttons in your application. |
2 | Canvas
The Canvas widget is used to draw shapes, such as lines, ovals, polygons and rectangles, in your application. |
3 | Checkbutton
The Checkbutton widget is used to display a number of options as checkboxes. The user can select multiple options at a time. |
4 | Entry
The Entry widget is used to display a single-line text field for accepting values from a user. |
5 | Frame
The Frame widget is used as a container widget to organize other widgets. |
6 | Label
The Label widget is used to provide a single-line caption for other widgets. It can also contain images. |
7 | Listbox
The Listbox widget is used to provide a list of options to a user. |
8 | Menubutton
The Menubutton widget is used to display menus in your application. |
9 | Menu
The Menu widget is used to provide various commands to a user. These commands are contained inside Menubutton. |
10 | Message
The Message widget is used to display multiline text fields for accepting values from a user. |
11 | Radiobutton
The Radiobutton widget is used to display a number of options as radio buttons. The user can select only one option at a time. |
12 | Scale
The Scale widget is used to provide a slider widget. |
13 | Scrollbar
The Scrollbar widget is used to add scrolling capability to various widgets, such as list boxes. |
14 | Text
The Text widget is used to display text in multiple lines. |
15 | Toplevel
The Toplevel widget is used to provide a separate window container. |
16 | Spinbox
The Spinbox widget is a variant of the standard Tkinter Entry widget, which can be used to select from a fixed number of values. |
17 | PanedWindow
A PanedWindow is a container widget that may contain any number of panes, arranged horizontally or vertically. |
18 | LabelFrame
A labelframe is a simple container widget. Its primary purpose is to act as a spacer or container for complex window layouts. |
19 | tkMessageBox
This module is used to display message boxes in your applications. |
让我们详细研究这些小部件-
让我们看一下如何指定它们的一些常见属性,例如大小,颜色和字体。
让我们简要地研究它们-
所有的Tkinter小部件都可以访问特定的几何管理方法,该方法的目的是在整个父小部件区域中组织小部件。 Tkinter公开以下几何图形管理器类:包装,网格和位置。
pack()方法-该几何管理器将小部件组织为块,然后再将其放置在父小部件中。
grid()方法-此几何管理器在父窗口小部件中以表格状结构组织窗口小部件。
place()方法-该几何管理器通过将小部件放置在父小部件中的特定位置来组织小部件。
让我们简要地研究几何管理方法-