第一次写此类代码,仅是个简单的框架,有待进一步丰富内容。源代码如下:
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()
运行效果截图:(实际运行时,两子窗口不能共存的)
