Tkinter一般用于界面设计,是Python自带的模块。它在实际开发中可能并不常用,用python写一些小工具时会用到。实际开发一般用的是Qt ,但Tkinter也可以很好地帮助理解GUI开发的逻辑。
以下是Tkinter的常用组件说明:
窗口组件
1 2 3 4 5 6 7 8 9 10 11 12
| from tkinter import *
window = Tk()
window.title("创建窗口")
window.mainloop()
|
按钮组件
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
| from tkinter import *
window = Tk() window.title("按钮组件") window.geometry('315x315')
lbtext = Label(window, text='点下面,有惊喜') lbtext.pack()
def clicked(): lbtext.configure(text="哈哈哈你被骗到啦,想屁吃")
btn = Button(window, text="万元好礼,点击就送", command=clicked) btn.pack()
window.mainloop()
|
标签组件
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
| from tkinter import * from tkinter import font
window = Tk() window.title("标签组件")
window.geometry('315x315')
lbtext = Label(window, text="月摇江湄,你回首花落梦碎。", font=('Arial Bold', 50), fg='red', bg='green', width=10, height=1)
lbtext.pack()
window.mainloop()
|
输入框组件
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
| from tkinter import *
window = Tk() window.title("文本框组件") window.geometry('315x315')
lbtext = Label(window, text='输入内容后请按下按钮') lbtext.pack()
txt = Entry(window, width=10) txt.pack()
txt.focus()
def clicked(): res = "Welcome to " + txt.get() if txt.get() != "": lbtext.configure(text=res)
btn = Button(window, text="走你!", command=clicked).pack()
window.mainloop()
|
单选框组件
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
| from tkinter import * from tkinter import font
window = Tk() window.title("标签组件")
window.geometry('315x315')
lbtext = Label(window, text="月摇江湄,你回首花落梦碎。", font=('Arial Bold', 50), fg='red', bg='green', width=10, height=1)
lbtext.pack()
window.mainloop()
|
对话框组件
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
| from tkinter import * from tkinter import font
window = Tk() window.title("标签组件")
window.geometry('315x315')
lbtext = Label(window, text="月摇江湄,你回首花落梦碎。", font=('Arial Bold', 50), fg='red', bg='green', width=10, height=1)
lbtext.pack()
window.mainloop()
|
复选框组件
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
| from tkinter import * from tkinter import font
window = Tk() window.title("标签组件")
window.geometry('315x315')
lbtext = Label(window, text="月摇江湄,你回首花落梦碎。", font=('Arial Bold', 50), fg='red', bg='green', width=10, height=1)
lbtext.pack()
window.mainloop()
|
进度条组件
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
| from tkinter import * from tkinter import ttk from tkinter.ttk import Progressbar
window = Tk() window.title("对话框组件") window.geometry('315x315')
style = ttk.Style() style.theme_use('default')
style.configure('black.Horizontal.TProgressbar', background='red') bar = Progressbar(window, length=200, style='black.Horizontal.TProgressbar')
bar['value'] = 50 bar.grid(column=0, row=1) window.mainloop()
|
数值调整组件
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
| from tkinter import *
window = Tk() window.title("对话框组件") window.geometry('315x315')
var = IntVar() var.set(10) spin = Spinbox(window, from_=0, to=10, width=5, textvariable=var) spin.grid(column=0, row=0)
window.mainloop()
|
文本区组件
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
| from tkinter import * from tkinter import scrolledtext
window = Tk() window.title("文本区组件") window.geometry('315x315')
txt = scrolledtext.ScrolledText(window, width=50, height=10)
def addtext(): txt.insert(INSERT, "当代价不再重要,人们也会对恐惧有所遗忘。")
def deltext(): txt.delete(1.0, END)
btn1 = Button(window, text='点击加入', command=addtext) btn2 = Button(window, text='点击清空', command=deltext)
txt.grid(column=0, row=0) btn1.grid(column=0, row=1) btn2.grid(column=0, row=2)
window.mainloop()
|
下拉框组件
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 28
| from tkinter import * from tkinter.ttk import Combobox
window = Tk() window.title("下拉框组件") window.geometry('315x315') lbtext = Label(window, text='点下面,有惊喜') lbtext.pack()
combo = Combobox(window)
combo['values'] = ('香辣虾', '烤鸡翅', '螃蟹粥', '流心月饼', '豆芽菜')
combo.current(0) combo.pack()
def clicked(): comtxt = combo.get() lbtext.configure(text=comtxt)
btn = Button(window, text='点我点我!!', command=clicked) btn.pack()
window.mainloop()
|
文件对话框组件
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
| import os from tkinter import * from tkinter import filedialog
window = Tk() window.title("对话框组件") window.geometry('315x315')
def opendir():
files = filedialog.askopenfilenames(filetypes=( ("Text files", "*.txt"), ("all files", "*.*")), initialdir=os.path.dirname(__file__))
btn = Button(window, text='选择文件', command=opendir) btn.pack()
window.mainloop()
|
菜单组件
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 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
| from re import sub from tkinter import *
window = Tk() window.title("菜单组件") window.geometry('315x315') lbtext = Label(window, text='菜单显示') lbtext.pack()
def do_job(): pass
menubar = Menu(window)
filemenu = Menu(menubar, tearoff=0)
menubar.add_cascade(label='File', menu=filemenu)
filemenu.add_cascade(label='New', command=do_job) filemenu.add_cascade(label='Open', command=do_job) filemenu.add_cascade(label='Save', command=do_job) filemenu.add_separator()
filemenu.add_cascade(label='Exit', command=window.quit)
editmenu = Menu(menubar, tearoff=0) menubar.add_cascade(label='Edit', menu=editmenu) editmenu.add_cascade(label='Cut', command=do_job) editmenu.add_cascade(label='Copy', command=do_job) editmenu.add_cascade(label='Paste', command=do_job)
submenu = Menu(filemenu) filemenu.add_cascade(label='Import', menu=submenu, underline=0) submenu.add_command(label='Submenu_1', command=do_job)
window.config(menu=menubar)
window.mainloop()
|
参考文章
https://www.cnblogs.com/shwee/p/9427975.html
https://likegeeks.com/python-gui-examples-tkinter-tutorial/