下面有一个由两个独立屏幕组成的简化代码。我该怎么做,这样当一个屏幕打开时,旧的屏幕就会关闭?
虽然我在这个例子中没有多个屏幕,但我希望能够在依赖GUI设计的6-9个屏幕上做到这一点。
如果能在这个问题上提供任何帮助,我们将不胜感激。
from tkinter import *
import os
import time
from tkinter import simpledialog
def login():
global screen2
screen2 = Toplevel(screen)
screen2.title("Login")
screen2.geometry("530x290")
Label(screen2, text = "please enter details below to login").pack()
Label(screen2, text = "").pack()
calibration1 = StringVar()
calibration2 = StringVar()
calibration3 = StringVar()
global calibration1_entry
global calibration2_entry
global calibration3_entry
calibration1_entry= Entry(screen2, textvariable = calibration1).place(x=350, y=70)
calibration2_entry = Entry(screen2, textvariable = calibration2).place(x=350, y=120)
calibration3_entry = Entry(screen2, textvariable = calibration3).place(x=350, y=170)
def main_screen():
global screen
screen = Tk()
screen.geometry("530x290")
screen.title("Remote Monitoring Site 1")
Label(text = "Remote Monitoring Site 1", bg = "grey", width = "300", height = "2", font = ("Calibri", 13)).pack()
Label(text = "").pack()
Button(text = "Login", width = "30", height = "2", command = login).pack()
Label(text = "").pack()
screen.mainloop()
main_screen()
您可以在开始时将screen2
分配为None
,并确保销毁并重新打开它,如果它不是None
:
def login():
global screen2
if screen2 is None: # if screen2 is not Toplevel
screen2 = Toplevel(screen)
else:
screen2.destroy() # destroy and start new screen
screen2 = Toplevel(screen)
...
...
def main_screen():
global screen2,screen
screen = Tk()
screen2 = None # Assign to None at the beginning
...
...
这也将确保没有两个Toplevel
同时打开。
您可以制作多个脚本,例如在一个脚本中对登录屏幕进行编码,在另一个脚本中将主菜单进行编码等。然后您可以使用import os
关闭和打开脚本。功能类似于:
def login():
if username == "admin" and password == "admin":
file = 'python3 main_menu.py' # or you can do it
root.destroy() # proper way with db
os.system(file)