Tkinter 编程
Tkinter 是 Python 的标准 GUI 库。Python 使用 Tkinter 可以快速的创建 GUI 应用程序。
由于 Tkinter 是内置到 python 的安装包中、只要安装好 Python 之后就能 import Tkinter 库、而且 IDLE 也是用 Tkinter 编写而成、对于简单的图形界面 Tkinter 还是能应付自如。
创建一个GUI程序
- 1、导入 Tkinter 模块
- 2、创建控件
- 3、指定这个控件的 master, 即这个控件属于哪一个
- 4、告诉 GM(geometry manager) 有一个控件产生了。
import tkinter top = tkinter.Tk() # 进入消息循环 top.mainloop()
# 导入 Tkinter 库 # Python3.x 导入方法 from tkinter import * root = Tk() # 创建窗口对象的背景色 # 创建两个列表 li = ['C','python','php','html','SQL','java'] movie = ['CSS','jQuery','Bootstrap'] listb = Listbox(root) # 创建两个列表组件 listb2 = Listbox(root) for item in li: # 第一个小部件插入数据 listb.insert(0,item) for item in movie: # 第二个小部件插入数据 listb2.insert(0,item) listb.pack() # 将小部件放置到主窗口中 listb2.pack() root.mainloop() # 进入消息循环
Tkinter 组件
Tkinter 的提供各种控件,如按钮,标签和文本框,一个GUI应用程序中使用。这些控件通常被称为控件或者部件。
目前有15种 Tkinter的部件。我们提出这些部件以及一个简短的介绍,在下面的表:
控件 | 描述 |
---|---|
Button | 按钮控件;在程序中显示按钮。 |
Canvas | 画布控件;显示图形元素如线条或文本 |
Checkbutton | 多选框控件;用于在程序中提供多项选择框 |
Entry | 输入控件;用于显示简单的文本内容 |
Frame | 框架控件;在屏幕上显示一个矩形区域,多用来作为容器 |
Label | 标签控件;可以显示文本和位图 |
Listbox | 列表框控件;在Listbox窗口小部件是用来显示一个字符串列表给用户 |
Menubutton | 菜单按钮控件,用于显示菜单项。 |
Menu | 菜单控件;显示菜单栏,下拉菜单和弹出菜单 |
Message | 消息控件;用来显示多行文本,与label比较类似 |
Radiobutton | 单选按钮控件;显示一个单选的按钮状态 |
Scale | 范围控件;显示一个数值刻度,为输出限定范围的数字区间 |
Scrollbar | 滚动条控件,当内容超过可视化区域时使用,如列表框。. |
Text | 文本控件;用于显示多行文本 |
Toplevel | 容器控件;用来提供一个单独的对话框,和Frame比较类似 |
Spinbox | 输入控件;与Entry类似,但是可以指定输入范围值 |
PanedWindow | PanedWindow是一个窗口布局管理的插件,可以包含一个或者多个子控件。 |
LabelFrame | labelframe 是一个简单的容器控件。常用与复杂的窗口布局。 |
tkMessageBox | 用于显示你应用程序的消息框。 |
标准属性
标准属性也就是所有控件的共同属性,如大小,字体和颜色等等。
属性 | 描述 |
Dimension | 控件大小; |
Color | 控件颜色; |
Font | 控件字体; |
Anchor | 锚点; |
Relief | 控件样式; |
Bitmap | 位图; |
Cursor | 光标; |
几何管理
Tkinter控件有特定的几何状态管理方法,管理整个控件区域组织,以下是Tkinter公开的几何管理类:包、网格、位置
几何方法 | 描述 |
pack() | 包装; |
grid() | 网格; |
place() | 位置; |
按钮示例
from tkinter import * from tkinter import messagebox top = Tk() top.geometry("100x100") def helloCallBack(): msg = messagebox.showinfo( "Hello Python", "Hello World") B = Button(top, text = "Hello", command = helloCallBack) B.place(x = 50,y = 50) top.mainloop()
Canvas 示例
from tkinter import * from tkinter import messagebox top = Tk() C = Canvas(top, bg = "blue", height = 250, width = 300) coord = 10, 50, 240, 210 arc = C.create_arc(coord, start = 0, extent = 150, fill = "red") line = C.create_line(10,10,200,200,fill = 'white') C.pack() top.mainloop()
Checkbutton
from tkinter import * import tkinter top = Tk() CheckVar1 = IntVar() CheckVar2 = IntVar() C1 = Checkbutton(top, text = "Music", variable = CheckVar1, \ onvalue = 1, offvalue = 0, height=5, \ width = 20, ) C2 = Checkbutton(top, text = "Video", variable = CheckVar2, \ onvalue = 1, offvalue = 0, height=5, \ width = 20) C1.pack() C2.pack() top.mainloop()
Entry
from tkinter import * top = Tk() L1 = Label(top, text = "User Name") L1.pack( side = LEFT) E1 = Entry(top, bd = 5) E1.pack(side = RIGHT) top.mainloop()
Frame
from tkinter import * root = Tk() frame = Frame(root) frame.pack() bottomframe = Frame(root) bottomframe.pack( side = BOTTOM ) redbutton = Button(frame, text = "Red", fg = "red") redbutton.pack( side = LEFT) greenbutton = Button(frame, text = "Brown", fg="brown") greenbutton.pack( side = LEFT ) bluebutton = Button(frame, text = "Blue", fg = "blue") bluebutton.pack( side = LEFT ) blackbutton = Button(bottomframe, text = "Black", fg = "black") blackbutton.pack( side = BOTTOM) root.mainloop()
Label
from tkinter import * root = Tk() var = StringVar() label = Label( root, textvariable = var, relief = RAISED ) var.set("Hey!? How are you doing?") label.pack() root.mainloop()
Listbox
from tkinter import * import tkinter top = Tk() Lb1 = Listbox(top) Lb1.insert(1, "Python") Lb1.insert(2, "Perl") Lb1.insert(3, "C") Lb1.insert(4, "PHP") Lb1.insert(5, "JSP") Lb1.insert(6, "Ruby") Lb1.pack() top.mainloop()
Menubutton
from tkinter import * import tkinter top = Tk() mb = Menubutton ( top, text = "condiments", relief = RAISED ) mb.grid() mb.menu = Menu ( mb, tearoff = 0 ) mb["menu"] = mb.menu mayoVar = IntVar() ketchVar = IntVar() mb.menu.add_checkbutton ( label = "mayo", variable = mayoVar ) mb.menu.add_checkbutton ( label = "ketchup", variable = ketchVar ) mb.pack() top.mainloop()
Menu
from tkinter import * def donothing(): filewin = Toplevel(root) button = Button(filewin, text="Do nothing button") button.pack() root = Tk() menubar = Menu(root) filemenu = Menu(menubar, tearoff = 0) filemenu.add_command(label="New", command = donothing) filemenu.add_command(label = "Open", command = donothing) filemenu.add_command(label = "Save", command = donothing) filemenu.add_command(label = "Save as...", command = donothing) filemenu.add_command(label = "Close", command = donothing) filemenu.add_separator() filemenu.add_command(label = "Exit", command = root.quit) menubar.add_cascade(label = "File", menu = filemenu) editmenu = Menu(menubar, tearoff=0) editmenu.add_command(label = "Undo", command = donothing) editmenu.add_separator() editmenu.add_command(label = "Cut", command = donothing) editmenu.add_command(label = "Copy", command = donothing) editmenu.add_command(label = "Paste", command = donothing) editmenu.add_command(label = "Delete", command = donothing) editmenu.add_command(label = "Select All", command = donothing) menubar.add_cascade(label = "Edit", menu = editmenu) helpmenu = Menu(menubar, tearoff=0) helpmenu.add_command(label = "Help Index", command = donothing) helpmenu.add_command(label = "About...", command = donothing) menubar.add_cascade(label = "Help", menu = helpmenu) root.config(menu = menubar) root.mainloop()
Message
from tkinter import * root = Tk() var = StringVar() label = Message( root, textvariable = var, relief = RAISED ) var.set("Hey!? How are you doing?") label.pack() root.mainloop()
Radiobutton
from tkinter import * def sel(): selection = "You selected the option " + str(var.get()) label.config(text = selection) root = Tk() var = IntVar() R1 = Radiobutton(root, text = "Option 1", variable = var, value = 1, command = sel) R1.pack( anchor = W ) R2 = Radiobutton(root, text = "Option 2", variable = var, value = 2, command = sel) R2.pack( anchor = W ) R3 = Radiobutton(root, text = "Option 3", variable = var, value = 3, command = sel) R3.pack( anchor = W) label = Label(root) label.pack() root.mainloop()
Scale
from tkinter import * def sel(): selection = "Value = " + str(var.get()) label.config(text = selection) root = Tk() var = DoubleVar() scale = Scale( root, variable = var ) scale.pack(anchor = CENTER) button = Button(root, text = "Get Scale Value", command = sel) button.pack(anchor = CENTER) label = Label(root) label.pack() root.mainloop()
Scrollbar
from tkinter import * root = Tk() scrollbar = Scrollbar(root) scrollbar.pack( side = RIGHT, fill = Y ) mylist = Listbox(root, yscrollcommand = scrollbar.set ) for line in range(100): mylist.insert(END, "This is line number " + str(line)) mylist.pack( side = LEFT, fill = BOTH ) scrollbar.config( command = mylist.yview ) mainloop()
Text
from tkinter import * root = Tk() text = Text(root) text.insert(INSERT, "Hello.....") text.insert(END, "Bye Bye.....") text.pack() text.tag_add("here", "1.0", "1.4") text.tag_add("start", "1.8", "1.13") text.tag_config("here", background = "yellow", foreground = "blue") text.tag_config("start", background = "black", foreground = "green") root.mainloop()
Toplevel
from tkinter import * root = Tk() root.title("hello") top = Toplevel() top.title("Python") top.mainloop()
Spinbox
from tkinter import * master = Tk() w = Spinbox(master, from_ = 0, to = 10) w.pack() mainloop()
PanedWindow
from tkinter import * m1 = PanedWindow() m1.pack(fill = BOTH, expand = 1) left = Entry(m1, bd = 5) m1.add(left) m2 = PanedWindow(m1, orient = VERTICAL) m1.add(m2) top = Scale( m2, orient = HORIZONTAL) m2.add(top) bottom = Button(m2, text = "OK") m2.add(bottom) mainloop()
LabelFrame
from tkinter import * root = Tk() labelframe = LabelFrame(root, text = "This is a LabelFrame") labelframe.pack(fill = "both", expand = "yes") left = Label(labelframe, text = "Inside the LabelFrame") left.pack() root.mainloop()
messagebox
from tkinter import * from tkinter import messagebox top = Tk() top.geometry("100x100") def hello(): messagebox.showinfo("Say Hello", "Hello World") B1 = Button(top, text = "Say Hello", command = hello) B1.place(x = 35,y = 50) top.mainloop()
REF
https://www.tutorialspoint.com/python3/python_gui_programming.htm
https://www.runoob.com/python/python-gui-tkinter.html