Python GUI——Tkinter~简单入门

本文介绍了Python的GUI库Tkinter的基础知识,包括导入模块、建立根窗口、添加窗口部件和进入事件循环。通过一个简单的按钮窗口示例,讲解了Tkinter的使用步骤。还探讨了Tkinter中的占位符%r和%s的区别,偏函数的概念及其在简化多参数函数调用中的作用,以及lambda表达式的应用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Tkinter

  • 简单介绍

   python-tk。然后在程序中导入这个模块,便可以利用Tkinter开发应用程序了,导入指令如下:from Tkinter import *或者impython-pport Tkinter推荐记忆和使用前者。      Tkinter是一个经典的Python GUI程序,它由TK GUI工具包包装而来,利用这个模块可以方便地开发一些跨平台的图形界面。在windows和Mac OS下的Python开发包里默认包含这个模块,无需额外安装,而在Unix下需要自己编译安装,指令如下:sudo apt-get install python-tk,然后就可以在程序中导入Tkinter模块并使用它进行应用程序开发,导入指令如下:

from Tkinter import *(推荐使用

或者

import Tkinter

  • Tkinter 使用

  Tkinter的使用一般分为四个步骤:

  1. 导入Tkinter模块Tkinter 使用
  2. 建立根窗口
  3. 添加窗口部件,如label,命令按钮,文本输入区域等,然后考虑如何将其放置于主窗口中
  4. 进入事件循环,即mainloop

  下面以一个例子1(建立一个带按钮的窗口界面)来解释说明以上步骤:  

#coding:utf-8
#!/usr/bin/python

from Tkinter import *

# 点击button时对应的操作
def say_hello():
    print '你好,gui !'

# 根窗口
root = Tk() 
root.title('window with command') #主窗口标题
root.geometry('400x200')  #主窗口大小,中间的为英文字母x

# 次级窗口
com = Button(root,text = '打招呼', command = say_hello) # 第一个参数root说明com按钮是root的孩子,text指按钮的名称,command指点击按钮时所执行的操作
com.pack(side = BOTTOM)  # 次级窗口的位置摆放位置

# 事件循环
root.mainloop()

  运行结果如下: 

                 
  Label 

        首先 root = Tk()产生一个根窗口,并通过title()和geometry()方法来设置根窗口的标题和大小,根窗口只应该创建一次,并且在其他窗口创建之前被创建。

  接着,创建次级窗口,这里是Button,其中的参数分别是指这个按钮是根窗口的孩子,按钮的名称,以及点击按钮时执行的相应的操作。pack()方法

  它告诉相应的Button窗口部件去调整自己的位置和尺寸,并且使用自己可见。其中的side参数指代窗口摆放的位置,通常有TOP, BOTTOM, LEFT, RIGHT四个参数,分别指代上下左右。


   通常情况下,用面向对象的方式来重写代码,这样可扩展性和修改性比较好,如下:

# coding:utf-8
#!env/usr/bin/python

from Tkinter import *

class App(object):
    def __init__(self,master):
        self.com = Button(master,text = '打招呼', command = self.say_hello)    
        self.com.pack(side = BOTTOM)
       
    def say_hello():
        print '你好,Gui!'

root = Tk()
root.title('window with command')
root.geometry('400x200')

app = App(root)
root.mainloop()

  • Tkinter窗口部件类

  Tkinter支持的核心窗口部件有15个,其列表如下,列表为转载

     

Abstract Describes the Tkinter widget set for constructing graphical user interfaces (GUIs) in the Python programming language. This publication is available in Web form1 and also as a PDF document2. Please forward any comments to tcc-doc@nmt.edu. Table of Contents 1. What is Tkinter?.......................................................................................................................3 2. A minimal application..............................................................................................................3 3. Definitions..............................................................................................................................4 4. Layout management.................................................................................................................5 4.1. The .grid() method....................................................................................................5 4.2. Other grid management methods...................................................................................6 4.3. Configuring column and row sizes.................................................................................7 4.4. Making the root window resizeable................................................................................8 5. Standard attributes...................................................................................................................8 5.1. Dimensions...................................................................................................................9 5.2. The coordinate system...................................................................................................9 5.3. Colors...........................................................................................................................9 5.4. Type fonts...................................................................................................................10 5.5. Anchors......................................................................................................................11 5.6. Relief styles.................................................................................................................12 5.7. Bitmaps.......................................................................................................................12 5.8. Cursors.......................................................................................................................12 5.9. Images........................................................................................................................14 5.10. Geometry strings........................................................................................................14 5.11. Window names...........................................................................................................15 5.12. Cap and join styles.....................................................................................................15 5.13. Dash patterns.............................................................................................................16 5.14. Matching stipple patterns............................................................................................16 6. The Button widget................................................................................................................17 7. The Canvas widget................................................................................................................19 7.1. Canvas coordinates......................................................................................................20 7.2. The Canvas display list................................................................................................20 7.3. Canvas object IDs........................................................................................................21 7.4. Canvas tags................................................................................................................21 1http://www.nmt.edu/tcc/help/pubs/tkinter/ 2http://www.nmt.edu/tcc/help/pubs/tkinter/tkinter.pdf 1 Tkinter reference New Mexico Tech Computer Center 7.5. CanvastagOrId arguments......................................................................................21 7.6. Methods on Canvas widgets........................................................................................21 7.7. Canvas arc objects.......................................................................................................26 7.8. Canvas bitmap objects.................................................................................................28 7.9. Canvas image objects..................................................................................................29 7.10. Canvas line objects.....................................................................................................29 7.11. Canvas oval objects....................................................................................................31 7.12. Canvas polygon objects..............................................................................................32 7.13. Canvas rectangle objects.............................................................................................34 7.14. Canvas text objects.....................................................................................................35 7.15. Canvas window objects..............................................................................................36 8. The Checkbutton widget......................................................................................................37 9. The Entry widget..................................................................................................................40 9.1. Scrolling an Entry widget............................................................................................43 10. The Frame widget................................................................................................................43 11. The Label widget................................................................................................................44 12. The LabelFrame widget......................................................................................................46 13. The Listbox widget............................................................................................................48 13.1. Scrolling a Listbox widget........................................................................................52 14. The Menu widget..................................................................................................................52 14.1. Menu item creation (coption) options.........................................................................55 14.2. Top-level menus.........................................................................................................56 15. The Menubutton widget......................................................................................................57 16. The Message widget............................................................................................................59 17. The OptionMenu widget.......................................................................................................60 18. The PanedWindow widget....................................................................................................61 18.1. PanedWindow child configuration options...................................................................63 19. The Radiobutton widget....................................................................................................64 20. The Scale widget................................................................................................................67 21. The Scrollbar widget........................................................................................................70 21.1. The Scrollbarcommand callback............................................................................72 21.2. Connecting a Scrollbar to another widget................................................................73 22. The Spinbox widget............................................................................................................73 23. The Text widget..................................................................................................................78 23.1. Text widget indices...................................................................................................80 23.2. Text widget marks....................................................................................................81 23.3. Text widget images...................................................................................................82 23.4. Text widget windows...............................................................................................82 23.5. Text widget tags.......................................................................................................82 23.6. Setting tabs in a Text widget......................................................................................83 23.7. The Text widget undo/redo stack..............................................................................83 23.8. Methods on Text widgets..........................................................................................84 24. Toplevel: Top-level window methods..................................................................................91 25. Universal widget methods.....................................................................................................93 26. Standardizing appearance...................................................................................................101 26.1. How to name a widget class......................................................................................102 26.2. How to name a widget instance.................................................................................102 26.3. Resource specification lines.......................................................................................102 26.4. Rules for resource matching......................................................................................103 27. Connecting your application logic to the widgets...................................................................104 28. Control variables: the values behind the widgets...................................................................104 29. Focus: routing keyboard input.............................................................................................106 New Mexico Tech Computer Center Tkinter reference 2 30. Events................................................................................................................................107 30.1. Levels of binding......................................................................................................108 30.2. Event sequences.......................................................................................................109 30.3. Event types..............................................................................................................109 30.4. Event modifiers........................................................................................................110 30.5. Key names...............................................................................................................111 30.6. Writing your handler: The Event class......................................................................113 30.7. The extra arguments trick..........................................................................................115 30.8. Virtual events...........................................................................................................116 31. Pop-up dialogs....................................................................................................................116 31.1. The tkMessageBox dialogs module..........................................................................116 31.2. The tkFileDialog module.....................................................................................118 31.3. The tkColorChooser module.................................................................................119
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值