Python tkinter库之Toplevel 子窗口与主窗口之间的联系
Python 之间 窗口 联系 tkinter
2023-09-14 09:01:28 时间
第一次写此类代码,仅是个简单的框架,有待进一步丰富内容。源代码如下:
import tkinter as tk
class 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()
运行效果截图:(实际运行时,两子窗口不能共存的)
相关文章
- 用Python画一棵带音乐的雪夜圣诞树
- python移动app开发_神奇的Kivy,让Python快速开发移动app
- java与python-如何对比Python和Java,只需三分钟告诉你!
- python中关于命名的例子_Python 命名规范入门实例「建议收藏」
- python常见运维脚本_Python运维常用脚本[通俗易懂]
- Python动态爬虫爬取京东商品评论[通俗易懂]
- python最好的开发工具_Python编译器
- python setattr函数_Python内置函数(53)——setattr
- 【说站】python字符串之间的运算
- 【说站】python中mainloop()方法的使用
- 【说站】python gevent的原理分析
- 【测试开发】python系列教程:第一个Python程序
- python进程模块
- python lambda表达式 if_Python学习-lambda表达式
- Python安装失败_python第三方库安装失败
- 使用四元数计算两个分子之间的RMSD(附Python代码)
- 太好玩了,我用 Python 做了一个 ChatGPT 机器人
- 【Python常用函数】一文让你彻底掌握Python中的cut函数
- 软件测试|Python数据可视化神器——pyecharts教程(三)
- 为什么requests不是python标准库?
- Python文件操作(一)
- Python爬虫之Js逆向案例-拼多多商品详情数据&拼多多商品列表数据&拼多多商品优惠券数据示例返回值说明
- python-Python与MongoDB数据库-使用Python执行MongoDB查询(二)
- python-Python与MongoDB数据库-使用Python执行MongoDB查询(三)
- Python算法:如何解决回文索引问题详解编程语言
- python的get set方法示例详解编程语言
- python_Day41_线程01详解编程语言
- Python_Day35_Socket模块3和hmac模块详解编程语言
- 开发Linux下Python编程:实现自己的开发梦想(linux下python)
- 使用Python编程连接MySQL数据库(python连mysql)
- Python简易操作MySQL入门教程(python操作mysql)
- 利用Neo4j和Python进行无缝图形数据库支持(neo4j python)
- Python写的创建文件夹自定义函数mkdir()