Tkinter介绍

Tkinter介绍

 

Python支持多个图形库,例如Qt、wxWidgets,等等。但是Python的标准GUI库是Tkinter。Tkinter是Tk Interface的缩写。Python提供了tkinter包,里面含有Tkinter接口。

 

开始写程序

 

要使用Tkinter,首先需要导入Python提供的tkinter模块:

 import tkinter as tk

 

这个语句导入tkinter模块,但为方便,为它定义了一个别名tk。

 

GUI系统中,普遍有一个控件(widget)的概念。控件就是指按钮、滚动条、文本框这些东西。特殊地,窗口也是一个控件,窗口可以容纳其它控件。在Tkinter中,用Frame类表示窗口。我们的程序可以直接创建一个Frame对象再向里面添加控件,但习惯上会从Frame类派生出Application类(也可以叫其它名字):

 class Application(tk.Frame):

 

 

接下来我们需要为Application编写构造函数__init__:

 

def  __init__( self , master = None ):
     tk.Frame.__init__( self , master)
     self .grid()
     self .createWidgets()
 

 

首先,__init__有两个参数。第一个是self,即Application对象本身。第二个是master,在Tkinter中,一个控件可能属于另一个控件,这时另一个控件就是这个控件的master。默认一个窗口没有master,因此master有None的默认值。

 

第二行tk.Frame.__init__(self, master)调用Application的父类Frame的__init__函数初始化Application类的Frame类部分。__init__函数的定义如下:

 __init__(self, master=None, cnf={}, **kw)

 

 

初始化Frame对象。master为Frame的父控件,默认为None;cnf尚不清楚;kw为命名关键字参数,可接受的参数将在后面提到。

 

因为这里不是直接用对象调用函数,又不是一般意义的创建对象(例如后面的quitButton),而是初始化自身的一部分,因此需要传入self。我们还传入了Application.__init__的master参数,作为Frame.__init__的master。

 

第三行self.grid(),显示窗口并使用grid布局。grid方法是从Frame继承来的。Tkinter中有一个概念叫布局(layout),就是控件的排列方式。除了grid(),还有pack等布局。

 

第四行self.createWidgets(),调用后面定义的createWidgets方法。

 

然后是createWidgets方法的定义:

 

def  createWidgets( self ):
     self .quitButton  =  tk.Button( self , text = 'Quit' , command = self .quit)
     self .quitButton.grid()
 

 

createWidgets函数的定义就不讲了,只有一个self参数。

 

第二行self.quitButton = tk.Button(self, text='Quit', command.self.quit),为Application类创建了一个quitButton属性,类型为Button。Button的构造函数如下:

 __init__(self, master=None, cnf={}, **kw

 

 

初始化Button对象。master为Button的父控件,默认为None;cnf尚不清楚;kw为命名关键字参数,可接受的参数有text(标准)、command(按钮特有),其它参数将在以后提到。

 

这里和前面不同,不是初始化自身,而是创建了quitButton属性,因此不需要写Button.__init__(...),而是直接写Button(...),也不需要像前面一样传入self。那为什么还要传入self呢?这个self并不是对应于Button.__init__中的self参数,而是master参数,表示按钮的父控件是窗口!

 

我们还传入了text和command两个命名关键字参数。text是显示在按钮上的文字,这里为'Quit';command为点击按钮时调用的函数,这里为Frame类的quit函数,quit函数会使程序退出。

 

第三行,self.quitButton.grid(),和前面一样,显示按钮并使用grid()布局。

 

然后就轮到创建Application对象了:

 

app  =  Application()
app.master.title  =  'First Tkinter'
app.mainloop()
 

 

第一行app = Application(),创建一个名为app的Application对象。

 

第二行app.master.title = 'First Tkinter',将窗口的标题设置为'First Tkinter'。当然可能会因为窗口太小,看不见标题。

 

第三行app.mainloop(),进入主循环。

 

现在运行你的程序,看看是不是点击Quit按钮就会退出了。当然你也可以通过点击窗口上方的关闭按钮退出。

 

主循环

 

GUI程序中有一个主循环(main loop)的概念。用伪代码的形式表现出来,是类似于这样的:

while  noQuitCommands:
     checkInput()
     updateWidgets()
 

 

主循环会一直执行,直到出现退出请求。在主循环中,会检查用户输入(例如鼠标、键盘等),并更新控件。这里,如果发现鼠标点击了按钮,就会执行按钮的command,而command是Frame.quit,执行command相当于执行了Frame.quit,循环就结束了,控件全部销毁,程序结束。

 

最终代码

 

下面是本课的最终代码,配上完整注释:

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# 导入tkinter包,为其定义别名tk
import  tkinter as tk
 
# 定义Application类表示应用/窗口,继承Frame类
class  Application(tk.Frame):
     # Application构造函数,master为窗口的父控件
     def  __init__( self , master = None ):
         # 初始化Application的Frame部分
         tk.Frame.__init__( self , master)
         # 显示窗口,并使用grid布局
         self .grid()
         # 创建控件
         self .createWidgets()
 
     # 创建控件
     def  createWidgets( self ):
         # 创建一个文字为'Quit',点击会退出的按钮
         self .quitButton  =  tk.Button( self , text = 'Quit' , command = self .quit)
         # 显示按钮,并使用grid布局
         self .quitButton.grid()
 
# 创建一个Application对象app
app  =  Application()
# 设置窗口标题为'First Tkinter'
app.master.title  =  'First Tkinter'
# 主循环开始
app.mainloop()

转载于:https://www.cnblogs.com/bruce0425/p/8303592.html

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Tkinter is the built-in GUI package that comes with standard python distributions. This means it is easy to get started right away, without any extra installation or configuration. Tkinter’s strength lies in its simplicity of use and its intuitive nature which makes it suited for programmers and non-programmers alike. Once you get started, you will be surprised to see how a few lines of code can produce powerful GUI applications. Tkinter GUI Application Development Hotshot helps you learn the art of GUI programming – building real-world, productive and fun applications like text editor, drum machine, game of chess, media player, drawing application and many more. Each subsequent project builds on the skills acquired in the previous project. Also, learn to write multi-threaded and multi layered applications using Tkinter. Get to know modern best practices involved in writing GUI programs. Tkinter GUI Application Development Hotshot comes with a rich source of sample codes that you can use in your own projects in any discipline of your choice. Starting with a high level overview of Tkinter that covers the most important concepts involved in writing a GUI application, the book then takes you through a series of real world projects of increasing complexity, developing one project per chapter. After you have developed five full projects, the book provides you with some bare-bone skeleton codes for a few functional but incomplete projects, challenging you to put your skills to test by completing them. Finally, you are provided with tips for writing reusable, scalable, and quality GUI code for larger projects. The appendices provide a quick reference sheet for Tkinter. What you will learn from this book Structure your programs in the model-view framework Persist your application data with object serialization Work with external libraries and Tkinter extensions Write multi-threaded GUI programs Re-factor code at every stage of application development Integrate your GUI applications to backend database Use networking with your Tkinter program Apply Internationalization to your GUI applications Develop a GUI program framework for maximum code reuse and rapid application development
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值