使用Python的Tkinter构建现代化GUI应用01 Tkinter简介

使用Python的Tkinter构建现代化GUI应用01 Tkinter简介

简介

We have learned in our previous book Python for Everyone, about the concepts in Python which are procedure oriented or object oriented.

在上一本书《Python for Everyone》中,我们已经了解了 Python 中面向过程或面向对象的概念。

However, these concepts will be used as indispensable salt for our next learning, which is related to Graphical User Interface (GUI).

不过,这些概念将成为我们下一步学习图形用户界面(GUI)不可或缺的 “盐”;

We are surrounded by GUI apps in day-to-day life. Whenever we are using our mobile phone/Desktop applications and accessing any app or software, the first thing which we look forward to is how to access these apps or software.

在日常生活中,我们被图形用户界面应用程序所包围。每当我们使用手机/桌面应用程序访问任何应用程序或软件时,我们首先关注的就是如何访问这些应用程序或软件。

Any app on mobile phone or a computer system consists of hardware and is controlled by an operating system.

手机或计算机系统中的任何应用程序都由硬件组成,并由操作系统控制;

The high-level languages will be sitting on top of the operating system and Python is no exception. So, in this chapter, we will learn about tkinter library in Python.

高级语言将位于操作系统之上,Python 也不例外。因此,在本章中,我们将学习 Python 中的 tkinter 库。

结构

In this chapter, we will discuss the following topics

  • Introduction to tkinter
  • Basic Python GUI Program
  • Some standard attributes of Python tkinter
  • GUI
  • Colors
  • Fonts
  • Anchors
  • Relief Styles
  • Bitmaps
  • Cursors
  • Python tkinter Geometry Manager

在本章中,我们将讨论以下主题

  • tkinter 简介
  • 基本 Python GUI 程序
  • Python tkinter 的一些标准属性
  • 图形用户界面
  • 颜色
  • 字体
  • 锚点
  • 浮雕样式
  • 位图
  • 光标
  • Python tkinter 几何管理器

目标

By the end of this chapter, the reader will learn about creating basic GUI Pythonprogram using the tkinter library.

本章结束时,读者将学会使用 tkinter 库创建基本的 GUI Python 程序。

In addition to that, we will also explore some standard attributes of Python tkinter GUI such as dimensions, colors or fonts with various options with examples.

除此之外,我们还将通过示例探索 Python tkinter GUI 的一些标准属性,如尺寸、颜色或字体等各种选项;

It is important to know how to position the text with a list of constants, relative to the reference point using anchor attribute. Moreover, we shall see how with the usage of relief attribute 3-D, simulated effects around the outside of the widget can be provided.

重要的是要知道如何使用锚点属性,通过常量列表相对于参考点定位文本。此外,我们还将看到如何使用浮雕属性 3-D,在 widget 外部提供模拟效果;

We will also learn about bitmap and cursor attribute with examples.

我们还将通过示例学习位图和光标属性;

Finally, at the end of this chapter, we will learn accessing tkinter widgets using inbuilt layout geometry managers viz pack, grid and place.

最后,在本章的最后,我们将学习使用内置的布局几何管理器(即 pack、grid 和 place)访问 tkinter 部件。

tkinter 简介

Whenever we write any program in Python to control the hardw are, Python willshow the output with the help of operating system.

每当我们用 Python 编写任何程序来控制硬盘时,Python 都会在操作系统的帮助下显示输出结果;

However, if we desire to make an executable with the help of GUI, then just having the need of hardware and operating system is insufficient.

但是,如果我们希望借助图形用户界面来制作可执行文件,那么仅仅需要硬件和操作系统是不够的;

Python requires some services which come from a number of resources and one such resource which is of interest to many Python programmers is Tcl/Tk.

Python 需要一些来自许多资源的服务,许多 Python 程序员感兴趣的资源之一就是 Tcl/Tk;

Tcl stands for Tool Command Language and it is a scripting language with its own interpreter.

Tcl 是工具命令语言的缩写,是一种脚本语言,有自己的解释器;

On the other hand, Tk is a toolkit for building GUIs. An important point to note is that Tcl/Tk is not Python and we cannot control and access the services of Tcl/Tk using Python. So, another package is introduced and is referred to as tkinter, and it is an intermediator between Python and Tcl/Tk. tkinter will allow us to use the services of Tcl/Tk using the syntax of Python.

另一方面,Tk 是用于构建图形用户界面的工具包。需要注意的一点是,Tcl/Tk 不是 Python,我们无法使用 Python 控制和访问 Tcl/Tk 的服务。因此,我们引入了另一个名为 tkinter 的软件包,它是 Python 和 Tcl/Tk 之间的中介。tkinter 允许我们使用 Python 的语法使用 Tcl/Tk 的服务;

As Python code developers, we will not be directly concerned with the Tcl/Tk. Binding of Python to the Tk GUI toolkit will be done by tkinter. tkinter will make everything appear as an object.

作为 Python 代码开发者,我们不会直接关注 Tcl/Tk。Python 与 Tk GUI 工具包的绑定将由 tkinter 完成;

We can create GUI, knowing that we can regard the window as an object, a label place on window as an object and so on.

我们在创建图形用户界面时,可以将窗口视为一个对象,将窗口上的标签视为一个对象,等等;

Applications can be built from a view point of an object-oriented programming paradigm. All we need to make sure is that we write our code in such a way that it allows us access tkinter, as shown in the following Figure 1.1:

可以从面向对象编程范例的角度来构建应用程序。我们只需确保在编写代码时允许我们访问 tkinter,如下图 1.1 所示:

使用Python的Tkinter构建现代化GUI应用01 Tkinter简介_第1张图片

So, GUI applications can be easily and quickly created when Python is combined with tkinter.

因此,当 Python 与 tkinter 结合使用时,图形用户界面应用程序就可以轻松快速地创建;

Although Python offers multiple options for developing GUI such as PyQT, Kivy, Jython, WxPython, and pyGUI; tkinter is the most commonly used.

尽管 Python 为开发图形用户界面提供了多种选择,如 PyQT、Kivy、Jython、WxPython 和 pyGUI,但 tkinter 是最常用的;

In this book, we will focus only on the tkinter usage of GUI creation.

在本书中,我们将只关注创建图形用户界面时使用 tkinter 的情况;

Just like we import any other module, tkinter can be imported in the same way in Python code:

就像我们导入其他模块一样,在 Python 代码中也可以用同样的方式导入 tkinter:

import tkinter

This will import the tkinter module.

这将导入 tkinter 模块。

The module name in Python 3.x is tkinter, whereas in Python 2.x, it is Tkinter.

Python 3.x 中的模块名为 tkinter,而 Python 2.x 中的模块名为 Tkinter。

More often, we can use:

更多时候,我们可以使用:

from tkinter import *

Here, the ‘*’ symbol means everything, as Python now can build Graphical Use rInterfaces by treating all of the widgets like Buttons, Labels, Menus and so on, as if they were objects. It is like importing tkinter submodule.

在这里,"*"符号意味着一切,因为 Python 现在可以通过将按钮、标签、菜单等所有部件视为对象来构建图形使用界面。这就像导入 tkinter 子模块一样。

However, there are some important methods which the user needs to know while creating Python GUI application, and they are as follows:

不过,在创建 Python GUI 应用程序时,用户需要了解一些重要的方法,具体如下:

Tk(screenName=None, baseName=None, className=’Tk’, useTk=1)

tkinter offers this method in order to create a main window. For any application, a main window code can be created by using:

tkinter 提供了创建主窗口的方法。对于任何应用程序,主窗口代码都可以通过使用

import tkinter
myroot = tkinter.Tk() 

Here, Tk class is instantiated without any arguments. myroot is the mai nwindow object name. This method will allow blank parent window creation with close, maximize and minimize buttons on the top.

myroot 是 mai nwindow 对象的名称。该方法将允许创建空白父窗口,并在顶部设置关闭、最大化和最小化按钮。

mainloop()

tkinter offers this method when our application is ready to run. This metho dis an infinite loop used to run the application. It will wait for an event to occur and as long as the window is not closed, the event is processed.

当我们的应用程序准备运行时,tkinter 会提供这种方法。该方法会产生一个用于运行应用程序的无限循环。它会等待事件发生,只要窗口没有关闭,事件就会被处理。

第一个Python GUI程序

Let us see a basic Python program which will create a window:

让我们看一个基本的 Python 程序,它将创建一个窗口:

from tkinter import *

# 创建一个窗口对象
myroot = Tk() 

# 启动事件循环,相当于启动GUI程序
myroot.mainloop()

输出效果如下:

使用Python的Tkinter构建现代化GUI应用01 Tkinter简介_第2张图片

说明

本文翻译自《Building Modern GUIs with Tkinter and Python Building user-friendly GUI applications with ease》一书,加上了一些自己的理解。特别是代码部分,可能会大量的重写,练习等。

如果想要原版电子书可以留言。

如果涉及到侵权,请联系我删掉。

如果您有想要翻译的英文书籍,请联系我,我可以代为翻译。

如果您想要学习更多的编程知识,可以购买我的视频课,直播课,或者私教课。

如果您有想要开发的软件项目,可以联系我,我可以代为开发。

如果您是学生,有解决不了的编程问题,可以联系我,我可以代为解决。

如果您是程序员,在企业内有解决不了的难题,可以联系我,兴许我可以提供一些建议。

我是张大鹏,”Python私教“的创始人,全栈工程师,著有zdppy和zdpgo两个跨语言的全栈开发框架,如果您对我的框架感兴趣,可以联系我,需要说明的是,这两个框架不是开源的,需要付费购买,但是可以试用,保证能够提高您的开发效率。

其他… 生活不易,如果您有闲钱,麻烦打赏我一点吧,1块不嫌少,20刚刚好,100不嫌多,1000… 就想想吧~

你可能感兴趣的:(python)