tkinter基本概念

tkinter基本概念

GUI: Graphical User Interface 图形用户接口

from tkinter import *

列出tkinter版本

import tkinter
print(tkinter.TkVersion)
建立窗口

通常将使用Tk( )方法建立的窗口称为根窗口,之后可以在此根窗口中建立许多控件,也可以在此根窗口中建立上层窗口。

建立空白窗口,窗口默认名称是tk

from tkinter import *
root = Tk()
root.mainloop()

窗口大小是默认大小,当窗口出现后,可以拖曳移动窗口或更改窗口大小

在GUI程序设计中,有时候也将上述所建立的窗口(window)称为容器(container)。

窗口属性的设置

与窗口相关的方法

image-20220506163915496

设置窗口标题为MyWindow,同时设置宽是300,高是160

from tkinter import *
root = Tk()
root.title("MyWindow")
root.geometry("300x160")
root.configure(bg="yellow")
root.mainloop()

除了可以使用名称直接设置色彩,还可以使用十六进制方式设置色彩RGB,其中每个色彩用两个十六进制数字表示。

使用mystar.ico更改系统默认的图标,同时使用另一种更改背景颜色的方法。

from tkinter import *
root = Tk()
root.configure(bg="#00ff00")
root.iconbitmap("mystar.ico")
root.mainloop()
窗口位置的设置

geometry( )方法除了可以设置窗口的大小,也可以设置窗口的位置,此时它的语法格式如下。

geometry(widthxheight+x+y)
  • widthxheight说明窗口的宽和高,width与height用x分隔

  • “+x”表示x是窗口左边距离屏幕左边的距离

    如果是“-x”,则表示x是窗口右边距离屏幕右边的距离

  • “+y”表示y是窗口上边距离屏幕上边的距离

    如果是“-y”则表示y是窗口下边距离屏幕下边的距离

获得屏幕的宽度和高度

屏幕 不是 窗口

root.winfo_screenwidth()
root.winfo_screenheight()
设计窗口时将此窗口放在屏幕中央
from tkinter import *
root = Tk()
screenWidth = root.winfo_screenwidth()
screenHeight = root.winfo_screenheight()
w = 300
h = 160
x = (screenWidth -w)/2
y = (screenHeight-h)/2
root.geometry("%dx%d+%d+%d"%(w,h,x,y))
root.mainloop()
tkinter的Widget

Widget可以翻译为控件或组件或部件。

窗口建立完成后,下一步是在窗口内建立控件,将这些控件统称为Widget。

  • Button(按钮)
  • Canvas(画布)
  • Checkbutton(多选按钮)
  • Entry(文本框)
  • Frame(框架)
  • Label(标签)
  • LabelFrame(标签框架)
  • Listbox(列表框)
  • Menu(菜单)
  • MenuButton(菜单按钮)
  • Message(消息)
  • OptionMenu(下拉式菜单)
  • PanedWindow(面板)
  • Radiobutton(单选按钮)
  • Scale(尺度)
  • Scrollbar(滚动条)
  • Spinbox(可微调输入控件)
  • Text(文字区域)
  • Toplevel(上层窗口)
Widget的共同属性
  • Dimensions:大小
  • Colors:颜色
  • Fonts:字形
  • Anchor:锚(位置参考点)
  • Relief styles:属性边框
  • Bitmaps:显示位图
  • Cursors:鼠标外形
Widget的共同方法
  1. Configuration

    • config(option=value)

      Widget属性可以在建立时设置,也可以在程序执行时使用config( )重新设置

    • cget(“option”)

      取得option参数值

    • keys( )

      可以用此方法获得所有该Widget的参数

  2. Event Processing

    • mainloop( )

      让程序继续执行,同时进入等待与处理窗口事件

    • quit( )

      Python Shell窗口结束,但是所建窗口继续执行

    • update( )

      更新窗口画面

  3. Event callbacks

    • bind(event,callback)

      事件绑定

    • unbind(event)

      解除绑定

  4. Alarm handlers

    • after(time,callback)

      间隔指定时间后调用callback( )方法

加强版的tkinter模块

tkinter在后来也推出了加强版的模块,称为tkinter.ttk,有时简称ttk

ttk模块新增的Widget

  • Combobox
  • Notebook
  • Progressbar
  • Separator
  • Sizegrip 可以拖曳最上层窗口右下方更改最上层窗口的大小。
  • Treeview
导入模块
from tkinter import ttk

如果使用下列方式导入ttk,可以覆盖原先tkinter的控件:

from tkinter import *
from tkiner.ttk import *

使用ttk可以有更好的外观,而且也可以跨平台使用,不过并没有100%兼容

例如,fg、bg参数或一些外观相关的参数tk和ttk是不相同。

ttk使用的是ttk.Style类别。

RGB色彩表

image-20220506181224525

image-20220506181325252

image-20220506181334799

image-20220506181344879

image-20220506181650887

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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

暴风雨中的白杨

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值