Python Tkinter教程–第1部分

Tkinter简介 (Introduction to Tkinter)

There are multiple ways to interact with a program: either through the command line or through GUI. GUI is a desktop application that provides a user interface that helps interact with the computer or any electronic device visually through graphical icons. This makes GUI achieve a wide user-base with our software.

与程序交互的方式有多种:通过命令行或通过GUI。 GUI是一个桌面应用程序,提供了用户界面,该用户界面可通过图形图标直观地与计算机或任何电子设备进行交互。 这使GUI可以通过我们的软件获得广泛的用户基础。

Python provides numerous packages like Kivy, PythonQt, wxPython, Tkinter, etc. for creating GUIs. Among these popular packages, the de-facto standard package supported by Python to build GUI applications is Tkinter.

Python提供了许多软件包(例如KivyPythonQtwxPython ,Tkinter等)来创建GUI。 在这些流行的软件包中,Python支持的用于构建GUI应用程序的事实上的标准软件包是Tkinter。

Tkinter is the standard and inbuilt Python GUI library that provides users an object-oriented interface to the Tk GUI toolkit. Tkinter module is the most preferred GUI building interface by programmers due to its simplicity and ease of use.

Tkinter是标准的内置Python GUI库,可为用户提供Tk GUI工具箱的面向对象的界面。 由于Tkinter模块的简单性和易用性,它是程序员最喜欢的GUI构建界面。

Programmers can create GUI apps rapidly using Tkinter. In this tutorial, we will be using Tkinter to create GUI applications.

程序员可以使用Tkinter快速创建GUI应用。 在本教程中,我们将使用Tkinter创建GUI应用程序。

创建一个示例窗口 (Creating a sample Window)

We just discussed that while using a GUI, a user interacts with the elements of the interface that are called “widgets”. Widgets like buttons and checkboxes allow the user to interact with the program. Other widgets like windows and frames are used as containers for placing other widgets. Tkinter is readily available with standard Python 3 + installation. So, the user need not worry about the installation of the module. The user needs to just import the module, using:

我们刚刚讨论过,在使用GUI时,用户与称为“小部件”的界面元素进行交互。 按钮和复选框之类的小部件允许用户与程序进行交互。 其他小部件(例如窗口和框架)用作放置其他小部件的容器。 Tkinter随标准的Python 3 +安装一起提供。 因此,用户无需担心模块的安装。 用户只需使用以下命令导入模块:

import tkinter

import tkinter

Using Tkinter, creating a sample window is super easy, regardless of the operating system you are using. We can quickly create a window by the following lines of code.

使用Tkinter,无论使用什么操作系统,创建示例窗口都非常容易。 我们可以通过以下几行代码快速创建一个窗口。

import tkinter
root_window = tkinter.Tk()
root_window.mainloop()

After the first step of importing the Tkinter package, the next step is to create the root window widget for any application. There should be only one main window widget in the interface. All other widgets will be of lower hierarchy than the root. Hence, the main root window widget has to be created prior to creating other widgets.

在导入Tkinter软件包的第一步之后,下一步就是为任何应用程序创建根窗口小部件。 界面中应该只有一个主窗口小部件。 所有其他小部件的层次结构都将比根目录低。 因此,必须在创建其他窗口小部件之前创建主根窗口窗口小部件。

As you see in the above code, we initialize the root’s `mainloop`. This calls the mainloop() function, which calls the endless loop of the main root window. This window will wait for any kind of user interaction until their quit() method is called, or in case the user closes the window. The above code creates a sample Window widget as follow:

如您在上面的代码中看到的,我们初始化了根的`mainloop`。 这将调用mainloop()函数,该函数将调用主根窗口的无限循环。 该窗口将等待任何类型的用户交互,直到调用他们的quit()方法,或者在用户关闭窗口的情况下。 上面的代码创建一个示例Window小部件,如下所示:

Output

输出量

创建标签小部件 (Creating a label widget)

Now, we have learned to creating a Windows widget. Next, let’s add a simple Label widget that displays a sample text to the main root window. The below example creates a simple label widget. We can create a label using the label class as follows:

现在,我们学习了如何创建Windows小部件。 接下来,让我们添加一个简单的Label小部件,该小部件将示例文本显示到主根窗口。 下面的示例创建一个简单的标签小部件。 我们可以使用label类创建标签,如下所示:

label_widget = tkinter.Label(root_window, text="Build your own GUI with Tkinter")

label_widget = tkinter.Label(root_window, text="Build your own GUI with Tkinter")

The label class takes 2 mandatory arguments namely the label’s desired parent widget(i.e. the root_window) and the text to be displayed by the label. Next, we can set the position of the label using the grid function as follows:

label类采用2个必填参数,即标签所需的父窗口小部件(即root_window)和要由标签显示的文本。 接下来,我们可以使用网格功能如下设置标签的位置:

import tkinter
root_window = tkinter.Tk()
label_widget = tkinter.Label(root_window, text="Build your own GUI with Tkinter")
label_widget.grid(column=1, row=1))
root_window.mainloop()

Below is the sample output that displays a window having the label we just created. Isn’t that simple?

下面的示例输出显示一个带有我们刚刚创建的标签的窗口。 这不是很简单吗?

Output

输出量

自定义标签小部件 (Customizing the label widget)

We can customize the font size of the label by changing the font style as below:

我们可以通过如下更改字体样式来自定义标签的字体大小:

label_widget = tkinter.Label(root_window, text="Build your own GUI with Tkinter", font=("Arial Italic", 40))

label_widget = tkinter.Label(root_window, text="Build your own GUI with Tkinter", font=("Arial Italic", 40))

As you see, we can use the font parameter to change the font size of widgets.

如您所见,我们可以使用font参数来更改小部件的字体大小。

Changing the Label Font using Tkintet
Output

输出量

自定义窗口的标题和大小 (Customizing title and size of window)

As seen in the above example, the window size is very small. We can set the default size of the window using geometry function, which takes the width and height of the window as parameters(in pixels).

如上例所示,窗口大小非常小。 我们可以使用几何函数设置窗口的默认大小,该函数将窗口的宽度和高度作为参数(以像素为单位)。

root_window.geometry('950x200')

root_window.geometry('950x200')

The above line of code resizes the window size to the above-mentioned parameters.

上面的代码行将窗口大小调整为上述参数。

Similarly, we can add title to the window widget using the title function. For example:

同样,我们可以使用title函数将标题添加到窗口小部件。 例如:

root_window.title("Tkinter tutorial")

root_window.title("Tkinter tutorial")

Let’s customize the window size and title by adding the above 2 lines of code to our existing application.

让我们通过将上述两行代码添加到我们现有的应用程序中来定制窗口的大小和标题。

Output

输出量

结论 (Conclusion)

We have created a bare minimum GUI having a window and a label widget using Tkinter, in a jiffy. Python provides the Tkinter library along with its installation. Building your first window is just a step away. So, what are you waiting for? Create a GUI for your software that can be used even by non-expert users by using Tkinter.

我们使用Tkinter创建了一个最低限度的最小GUI,它具有一个窗口和一个标签小部件。 Python提供了Tkinter库及其安装。 建立您的第一个窗口仅一步之遥。 那你还在等什么? 使用Tkinter为您的软件创建一个GUI,即使非专业用户也可以使用。

翻译自: https://www.journaldev.com/33363/python-tkinter-tutorial-1

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值