赞
踩
-
-
- import tkinter as tk
- #第一种直接写个类
- class OneUi:
- def __init__(self,master:tk.Tk):
- self.top=tk.Toplevel(master)
- self.top.geometry('600x400+100+400')
- # self.top.overrideredirect(True)
- self.top['bg']='cyan'
- self.creat_widgets()
-
- def creat_widgets(self):
- self.label=tk.Label(self.top,text='minmin')
- self.label.pack()
-
- def exists(self):
- return self.top.winfo_exists()
-
- def show_top(self):
- return self.top.deiconify()
-
-
-
- class MainWindow:
- def __init__(self,root:tk.Tk):
- self.root=root
-
- self.btn=tk.Button(self.root,text='btn_click',command=self.show_oneui).pack()
- def show_oneui(self):
- if not hasattr(self.root,'child_window') or not self.root.child_window.exists():
- self.root.child_window=OneUi(self.root)
- else:
- self.root.child_window.show_top()
-
-
- if __name__ == '__main__':
- root=tk.Tk()
- root.geometry('600x500')
- MainWindow(root)
-
- root.mainloop()
-
- #第二个用继承
- import threading
- import time
- import tkinter as tk
-
- class OneUi(tk.Toplevel):
- def __init__(self,master:tk.Tk):
- super(OneUi, self).__init__(master)
- self.geometry('600x400+100+400')
- # self.top.overrideredirect(True)
- self['bg']='cyan'
- self.creat_widgets()
-
- def creat_widgets(self):
- self.label=tk.Label(self,text='minmin')
- self.label.pack()
-
-
-
-
- class MainWindow:
- def __init__(self,root:tk.Tk):
- self.root=root
- self.btn=tk.Button(self.root,text='btn_click',command=self.show_oneui).pack()
- def show_oneui(self):
- if not hasattr(self.root,'child_window') or not self.root.child_window.winfo_exists():
- self.root.child_window=OneUi(self.root)
- else:
- self.root.child_window.deiconify()
-
-
-
-
-
- if __name__ == '__main__':
-
-
-
-
- root=tk.Tk()
- root.geometry('600x500')
- MainWindow(root)
-
- root.mainloop()
-
-
- #第三种
- import tkinter as tk
-
- class Ui:
- def setupUi(self,root:tk.Tk):
- root.geometry("900x800")
- root.config(background="cyan")
-
- self.btn = tk.Button(root,text="btn",command=self.on_btn_click)
- self.btn.pack()
- self.w:tk.Toplevel = None
-
-
- def on_close(self):
- if self.w:
- self.w.destroy()
- self.w = None
-
-
-
- def on_btn_click(self):
- if self.__getattribute__("w") is not None:
- self.w.deiconify()
-
- else:
- self.w = tk.Toplevel(root)
- self.w.protocol("WM_DELETE_WINDOW", self.on_close)
-
-
-
-
- if __name__ == '__main__':
- root = tk.Tk()
- ui = Ui()
- ui.setupUi(root)
-
- root.mainloop()
![](https://csdnimg.cn/release/blogv2/dist/pc/img/newCodeMoreWhite.png)
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。