本文将简要介绍Tkinter如何开发带有界面的小软件,涉及到的功能包含界面布局,Button、CheckButton、RadioButton、Entry,事件绑定等;
下一篇将介绍如何使用Pyinstaller打包成Window下可以执行的EXE文件
Pyinstaller打包Tkinter
布局采用Grid结合Pack的方式
示例:
fm0 = Frame(master)
Label(fm0, text='').grid(row=0, column=0)
Radiobutton(fm0, text='试验参数',justify='left').grid(row=1, column=0)
fm0.pack(side='left',fill='both',expand='YES') #这里面的参数可以自行百度
fm1= Frame(master)
Label(fm1, text='').grid(row=0, column=1)
Radiobutton(fm1, text='O',justify='left').grid(row=1, column=0)
fm1.pack(side='left',expand='YES',fill='both')
这里面用到的控件用法如下:
1. 标题文字
Label(fm0, text='A')
2. 单选按钮
Radiobutton(fm0, text='试验参数',justify='left')
3. 输入框
Entry(fm0,textvariable=self.iuto)
4. 复选按钮
Checkbutton(fm0, text='K',command=self.callCB,onvalue=1,variable=self.CheckVar1) #这里的command是绑定了一个事件,这个待会会讲
5. 文本框
Text(fm1, width=50, height=15)
6. 插入图片
img=PhotoImage(file="p3.PNG")
text = Text(fm1, width=50, height=15)
text.image_create(END, image=img)
7. 弹窗:tkinter.messagebox支持各种各样的弹窗,这里只举个例子
import tkinter.messagebox as msg
msg.askokcancel("提示","这个是弹窗")
和前端一样,所有的控件都可以绑定事件,比如点击了按钮后会触发某一个操作,用法如下:
#控件
Checkbutton(fm0, text='K',command=self.callCB,onvalue=1,variable=self.CheckVar1).grid(row=14, column=0)
#事件
def callCB(self):
if self.CheckVar1.get()==1:
print("1")
else:
print("2")
from tkinter import * #注意模块导入方式,否则代码会有差别
import tkinter.messagebox as msg
class App:
def __init__(self, master):
self.master=master
#定义输入输出变量:
#输入
self.iload= StringVar()
self.iuto = StringVar()
self.rp = StringVar()
self.c1 = StringVar()
self.c2 = StringVar()
self.cx = StringVar()
self.c5 = StringVar()
self.c6 = StringVar()
self.ceq = StringVar()
self.leq = StringVar()
self.CheckVar1 = IntVar()
#输出
self.lload, self.deltauto, self.uto = StringVar(), StringVar(), StringVar()
#控制计算第几个等式,默认计算第一个
self.formate = 1
self.img=PhotoImage(file="p3.PNG")
#使用Frame增加一层容器
fm0 = Frame(master)
Label(fm0, text='').grid(row=0, column=0)
Radiobutton(fm0, text='试验参数 ',justify='left').grid(row=1, column=0)
Label(fm0, text='A').grid(row=2,column=0)
Label(fm0, text='B').grid(row=3,column=0)
Entry(fm0,textvariable=self.iuto).grid(row=2, column=1)
Entry(fm0,textvariable=self.iload).grid(row=3, column=1)
Label(fm0, text='C').grid(row=2, column=2)
Label(fm0, text='D').grid(row=3, column=2)
Label(fm0, text='').grid(row=4, column=0)
Label(fm0, text='').grid(row=5, column=0)
Radiobutton(fm0, text='E ',justify='left').grid(row=6, column=0)
Label(fm0, text='F').grid(row=7, column=0)
Label(fm0, text='G').grid(row=8, column=0)
Label(fm0, text='H').grid(row=9, column=0)
Label(fm0, text='I').grid(row=10, column=0)
Label(fm0, text='J').grid(row=11, column=0)
Label(fm0, text='').grid(row=12, column=0)
Label(fm0, text='').grid(row=13, column=0)
Checkbutton(fm0, text='K',command=self.callCB,onvalue=1,variable=self.CheckVar1).grid(row=14, column=0)
Label(fm0, text='L').grid(row=15, column=0)
Label(fm0, text='M').grid(row=16, column=0)
Label(fm0, text='N').grid(row=17, column=0)
Entry(fm0,textvariable=self.leq).grid(row=7, column=1)
Entry(fm0,textvariable=self.ceq).grid(row=8, column=1)
Entry(fm0,textvariable=self.rp).grid(row=9, column=1)
Entry(fm0,textvariable=self.c1).grid(row=10, column=1)
Entry(fm0,textvariable=self.c2).grid(row=11, column=1)
Entry(fm0, textvariable=self.cx).grid(row=15, column=1)
Entry(fm0, textvariable=self.c5).grid(row=16, column=1)
Entry(fm0, textvariable=self.c6).grid(row=17, column=1)
Label(fm0, text='H').grid(row=7, column=2)
Label(fm0, text='F').grid(row=8, column=2)
Label(fm0, text='Ω').grid(row=9, column=2)
Label(fm0, text='F').grid(row=10, column=2)
Label(fm0, text='F').grid(row=11, column=2)
Label(fm0, text='F').grid(row=15, column=2)
Label(fm0, text='F').grid(row=16, column=2)
Label(fm0, text='F').grid(row=17, column=2)
#Label(fm00, text='ur', image=self.img).grid(row=1, column=3)
fm0.pack(side='left',fill='both',expand='YES')
fm1= Frame(master)
Label(fm1, text='').grid(row=0, column=1)
Radiobutton(fm1, text='O',justify='left').grid(row=1, column=0)
#Label(fm1, text='').grid(row=2, column=1)
text = Text(fm1, width=50, height=15)
text.grid(row=3, column=1, sticky=W,columnspan=15)
#text.pack(side=TOP,anchor=W,fill=X,expand=YES)
text.image_create(END, image=self.img)
self.master.grid_columnconfigure(12, minsize=3)
#输出部分
Label(fm1, text='').grid(row=10, column=1)
Radiobutton(fm1, text='P ',justify='left').grid(row=11, column=0)
Label(fm1, text='Q ').grid(row=12, column=1)
Label(fm1, text='R ').grid(row=13, column=1)
Label(fm1, text='S ').grid(row=14, column=1)
Entry(fm1, text="T",textvariable=self.lload, width=30).grid(row=12, column=2, sticky=W)
Entry(fm1, text="U",textvariable=self.uto, width=30).grid(row=13, column=2, sticky=W)
Entry(fm1, text="V",textvariable=self.deltauto, width=30).grid(row=14, column=2, sticky=W)
Label(fm1, text='W').grid(row=12, column=3, sticky=N+W)
Label(fm1, text='X').grid(row=13, column=3, sticky=N+W)
Label(fm1, text='Y').grid(row=14, column=3, sticky=N+W)
Label(fm1, text='').grid(row=15, column=0)
Button(fm1, text="计算", command=self.compute, bd=6,height = 1,
width = 8).grid(row=17, column=1,sticky=N+S)
Button(fm1, text="退出", command=master.quit, bd=6,height = 1,
width = 8).grid(row=17, column=2,sticky=N+S)
#Label(fm00, text='ur', image=self.img).grid(row=1, column=3)
fm1.pack(side='left',expand='YES',fill='both')
def callCB(self):
'''
控制走第几个等式
'''
print("复选结果是:{}".format(self.formate))
if self.CheckVar1.get()==1:
self.formate=2
else:
self.formate=1
def compute(self):
if self.formate==1:
self.compute1()
else:
self.compute2()
def compute1(self):
'''
计算电路值
'''
print("计算1")
if not self.iload.get() or not self.iuto.get() or not self.rp.get() or not self.ceq.get() or not self.c1.get() or not self.c2.get() or not self.leq.get():
self.lload.set("请确认正确输入")
self.deltauto.set("请确认正确输入")
self.uto.set("请确认正确输入")
else:
self.uto.set(1)
self.lload.set(1)
self.deltauto.set(1)
self.messagebox()
def compute2(self):
'''
计算电路值
'''
print("计算2")
if not self.iload.get() or not self.iuto.get() or not self.rp.get() or not self.ceq.get() or not self.c1.get() or not self.c2.get() or not self.leq.get():
self.lload.set("请确认正确输入")
self.deltauto.set("请确认正确输入")
self.uto.set("请确认正确输入")
else:
self.uto.set(1)
self.lload.set(1)
self.deltauto.set(1)
self.messagebox()
def messagebox(self):
'''
弹窗部分
'''
msg.askokcancel("提示","这个是弹窗")
if __name__=="__main__":
root = Tk()
root.title("Tkinter测试") #设置标题
root.geometry("850x480") #设置窗口大小
display = App(root)
root.mainloop()