Python学习笔记7--Tkinter开发小软件(1)

本文将简要介绍Tkinter如何开发带有界面的小软件,涉及到的功能包含界面布局,Button、CheckButton、RadioButton、Entry,事件绑定等;
下一篇将介绍如何使用Pyinstaller打包成Window下可以执行的EXE文件
Pyinstaller打包Tkinter

界面效果

在这里插入图片描述

布局

布局采用Grid结合Pack的方式

  • Grid这个比较简单,将页面分成不同格子,把内容填到格子里
  • Pack能控制整体的框架位置,比如我的界面分为两部分,左边用一个fm0,右边是fm1,每一个fm里面用Grid的方式做填充
示例:
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()
  • 2
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
python开发的真实星空显示软件 含真实恒星位置数据3144颗 代码讲解见: https://blog.csdn.net/xiaorang/article/details/106598307 数据格式例: {'long': 0.023278328898474372, 'lat': -0.09961466705757636, 'light': 46, 'const': 66}, {'long': 0.024870941840919196, 'lat': 0.2338062439126301, 'light': 55, 'const': 62}, {'long': 0.028107061526797, 'lat': 1.1204335039257496, 'light': 56, 'const': 18}, {'long': 0.03660100303760025, 'lat': 0.5077259659824991, 'light': 21, 'const': 1}, {'long': 0.04004802831028905, 'lat': 1.0323574005393255, 'light': 23, 'const': 18}, {'long': 0.03944444109507185, 'lat': 0.3178583859888262, 'light': 55, 'const': 62}, {'long': 0.040797071265367454, 'lat': -0.488478858963941, 'light': 54, 'const': 74}, {'long': 0.0410661312228549, 'lat': -0.798444499556106, 'light': 39, 'const': 64}, {'long': 0.043800486202076855, 'lat': 0.1945266317121166, 'light': 55, 'const': 66}, {'long': 0.045036755271142, 'lat': 0.804111967609767, 'light': 50, 'const': 1}, {'long': 0.043785947609407745, 'lat': -1.4350775693910554, 'light': 53, 'const': 58}, {'long': 0.04915283505929031, 'lat': -0.2699684886295715, 'light': 49, 'const': 21}, {'long': 0.050498187206605094, 'lat': -0.4851966800391031, 'light': 54, 'const': 74}, {'long': 0.05119631890740283, 'lat': -0.6131874860342564, 'light': 52, 'const': 74}, {'long': 0.05775584219505068, 'lat': 0.26500400429202875, 'light': 28, 'const': 62}, {'long': 0.05896303407877759, 'lat': 0.7162006931179011, 'light': 57, 'const': 1}, {'long': 0.06371905629046214, 'lat': 0.3526728525507925, 'light': 48, 'const': 62}, {'long': 0.06387905062299246, 'lat': -0.33043929519585447, 'light': 44, 'const': 21}, 代码解说详细的教程见: https://blog.csdn.net/xiaorang/article/details/106598307
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值