
2021-04-14 Python tkinter库之Toplevel 子窗口与主窗口之间的联系
发布日期:2021-05-04 07:31:34
浏览次数:26
分类:技术文章
本文共 4739 字,大约阅读时间需要 15 分钟。
第一次写此类代码,仅是个简单的框架,有待进一步丰富内容。源代码如下:
import tkinter as tkclass Win1Input(tk.Toplevel): def __init__(self): super().__init__() self.title(u'凭证号录入') self.geometry('240x150+266+483') self.attributes("-toolwindow", 2) self.resizable(False,False) self.wm_attributes('-topmost',True) self.protocol("WM_DELETE_WINDOW", self.cancel) self.setup_UI() def setup_UI(self): global flagWin1 flagWin1=True self.code1 = tk.StringVar() self.code2 = tk.IntVar() te1=tk.Entry(self, textvariable=self.code1, width=20) te2=tk.Entry(self, textvariable=self.code2, width=20) te1.place(x=70,y=5) te2.place(x=70,y=35) tl1=tk.Label(self, text=u'凭证号:', width=8, anchor=tk.E) tl2=tk.Label(self, text=u'协议号:', width=8, anchor=tk.E) tl1.place(x=5,y=5) tl2.place(x=5,y=35) tb1=tk.Button(self, text=u"确定", command=self.ok) tb2=tk.Button(self, text=u"取消", command=self.cancel) tb1.place(x=60,y=80) tb2.place(x=150,y=80) te1.focus() def exit1(self): global flagWin1 flagWin1=False print('exitWin1') self.destroy() def ok(self): self.Retcode = [self.code1.get(), self.code2.get()] self.exit1() def cancel(self): self.Retcode = None self.exit1()class Win2Print(tk.Toplevel): def __init__(self): super().__init__() self.title(u'凭证打印') self.geometry('400x300+266+483') self.attributes("-toolwindow", 2) self.resizable(False,False) self.wm_attributes('-topmost',True) self.protocol("WM_DELETE_WINDOW", self.cancel) self.setup_UI() def setup_UI(self): global flagWin2 flagWin2=True tcv=tk.Canvas(self, width=400, height=220, bg='white') tcv.create_text((20, 80), text=u'凭证打印的内容,此处略', anchor=tk.W) tcv.place(x=0,y=0) tb1=tk.Button(self, text=u"打印", command=self.ok) tb2=tk.Button(self, text=u"取消", command=self.cancel) tb1.place(x=90,y=250) tb2.place(x=200,y=250) def exit2(self): global flagWin2 flagWin2=False print('exitWin2') self.destroy() def ok(self): self.Retcode = "Print OK" self.exit2() def cancel(self): self.Retcode = None self.exit2()class AppMain(tk.Tk): def __init__(self): super().__init__() global flagWin1,flagWin2 flagWin1=flagWin2=False self.title(u'房贷记账小助手') self.geometry('240x480+10+480') self.resizable(False,False) self.wm_attributes('-topmost',True) self.code1 = 'T1234567' self.code2 = 123456789 self.protocol("WM_DELETE_WINDOW", self.exitMainApp) #点击关闭按钮触发 self.setup_UI() def setup_UI(self): tl1=tk.Label(self, text=u'凭证号:', width=8, anchor=tk.E) tl2=tk.Label(self, text=u'协议号:', width=8, anchor=tk.E) tl1.place(x=5,y=5) tl2.place(x=5,y=35) self.tl3 = tk.Label(self, text=self.code1, width=21, anchor=tk.W) self.tl4 = tk.Label(self, text=self.code2, width=21, anchor=tk.W) self.tl3.place(x=70,y=5) self.tl4.place(x=70,y=35) tb1=tk.Button(self, text=u"录入", command=self.setup_config) tb1.place(x=50,y=100) tb2=tk.Button(self, text=u"打印", command=self.setup_config2) tb2.place(x=120,y=100) def setup_config(self): global flagWin1,flagWin2 if flagWin1: self.Input1.focus() return if flagWin2: self.Print1.focus() return res = self.get_voucherNum() if res is None: return self.code1, self.code2 = res self.tl3.config(text=self.code1) self.tl4.config(text=self.code2) def setup_config2(self): global flagWin1,flagWin2 if flagWin1: self.Input1.focus() return if flagWin2: self.Print1.focus() return res = self.print_voucher() if res is None: return def get_voucherNum(self): '''对应Win1Input窗口类''' self.Input1 = Win1Input() self.wait_window(self.Input1) try: return self.Input1.Retcode except: pass def print_voucher(self): '''对应Win2Print窗口类''' print('print') self.Print1 = Win2Print() self.wait_window(self.Print1) try: return self.Print1.Retcode except: pass def exitMainApp(self): '''点击关闭按钮触发''' print(flagWin1,flagWin2) #退出时子窗口Win1,Win2是否打开 if flagWin1 or flagWin2: return #判断子窗口是否关闭 print('exit(0)') self.destroy()if __name__ == '__main__': app = AppMain() app.mainloop()
运行效果截图:(实际运行时,两子窗口不能共存的)
发表评论
最新留言
网站不错 人气很旺了 加油
[***.192.178.218]2025年04月05日 05时07分02秒
关于作者

喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!
推荐文章
Thinkphp6.0+vue个人虚拟物品发卡网站源码
2019-03-03
手游服务端框架之关于玩家数据的解决方案
2019-03-03
游戏服务端框架之网关
2019-03-03
游戏服务端框架之模仿SpringMvc实现消息路由
2019-03-03
动态摇动吊牌自适应网站404页面源码
2019-03-03
炫酷文字消失动画网站404页面源码
2019-03-03
EMLOG模板山河网站主题分享
2019-03-03
2020年,51Talk求一个盈利的机会
2019-03-03
2019数字音乐市场年度回顾,QQ音乐全面领先
2019-03-03
迅雷新财报背后:下载一哥到艰难求生
2019-03-03
腾讯终于要杀入电商直播了
2019-03-03
花1亿扶持优质红人,如涵推动网红经济出圈之路有何深意?
2019-03-03
开门红财报下,贝壳找房的春天依然有点冷
2019-03-03
虾米逝去:透视在线音乐的下一场战争
2019-03-03
抢滩抖音、B站,快手港股IPO进程加速
2019-03-03
智能穿戴的结局依然充满悬念
2019-03-03
Linux中的虚拟内存机制和内存映射
2019-03-03
Android系统启动系列5 SystemServer进程下
2019-03-03
Android四大组件系列9 ContentProvider原理
2019-03-03
理解PendingIntent
2019-03-03