通过菜单打开 windows 和类似的菜单栏,并使用 Tkinter 销毁之前的 windows
Opening windows with similar menu bars though the menu and destroying previous windows using Tkinter
我正在尝试创建一个 Tkinter 管理系统,其页面可通过菜单打开。打开页面时,我想销毁上一页并打开一个新页面(所有这些都通过菜单按钮),但我没有成功。请问,有人可以帮我编辑代码吗?我会很感激的。我一直在努力研究代码,直到我感到困惑。
from tkinter import *
root= Tk()
root.geometry("1200x900")
root.title("Hospital Management System")
root.iconbitmap(r'C:\Users\KEN\electron-api-demos\development\grad.ico')
#=========================functions for switching the windows========
def HR_change():
HR=Tk()
HR.geometry("1200x900")
HR.title("HR")
HR.grid()
Schain.destroy()
root.destroy()
def Schain_change():
Schain=Tk()
Schain.geometry("1200x900")
Schain.title("SUPPLY CHAIN")
Schain.grid()
HR.destroy()
root.destroy()
# menus for the root
menubar= Menu(root)
File=Menu(menubar, tearoff=0)
File.add_command(label='Open File')
File.add_command(label='Save')
File.add_command(label='Exit')
menubar.add_cascade(label="File", menu=File)
Departments=Menu(menubar, tearoff=0)
Departments.add_command(label='Human Resource Department',command=HR_change)
Departments.add_command(label='Supply chain Department',command=Schain_change)
menubar.add_cascade(label="Departments", menu=Departments)
Help=Menu(menubar, tearoff=0)
Help.add_command(label='About ihms')
Help.add_command(label='Manual')
menubar.add_cascade(label="Help", menu=Help)
Settings=Menu(menubar, tearoff=0)
Settings.add_command(label='Log Out')
menubar.add_cascade(label="Settings", menu=Settings)
#configure menu for root
root.config(menu=menubar)
# menus for the HR DPT
menubar= Menu(HR)
File=Menu(menubar, tearoff=0)
File.add_command(label='Open File')
File.add_command(label='Save')
File.add_command(label='Exit')
menubar.add_cascade(label="File", menu=File)
Departments=Menu(menubar, tearoff=0)
Departments.add_command(label='Human Resource Department',command=HR_change)
Departments.add_command(label='Supply chain Department',command=Schain_change)
menubar.add_cascade(label="Departments", menu=Departments)
Help=Menu(menubar, tearoff=0)
Help.add_command(label='About ihms')
Help.add_command(label='Manual')
menubar.add_cascade(label="Help", menu=Help)
Settings=Menu(menubar, tearoff=0)
Settings.add_command(label='Log Out')
menubar.add_cascade(label="Settings", menu=Settings)
#configure menu for root
HR.config(menu=menubar)
# content in HR window pages
name=Label(HR, text="Name")
name.grid(row=1, column=0, padx=10, pady=2, sticky=W)
nameentry=Entry(HR, width=40)
nameentry.grid(row=1, column=1, padx=10, pady=20)
Age=Label(HR, text="Age")
Age.grid(row=2, column=0, padx=10, pady=20, sticky=W)
Ageentry=Entry(HR, width=40)
Ageentry.grid(row=2, column=1, padx=10, pady=20)
NatID=Label(HR, text="National ID")
NatID.grid(row=3, column=0, padx=10, pady=2, sticky=W)
NatIDentry=Entry(HR, width=40)
NatIDentry.grid(row=3, column=1, padx=10, pady=20)
Jobpos=Label(HR, text="Job Position")
Jobpos.grid(row=4, column=0, padx=10, pady=20, sticky=W)
Jobposentry=Entry(HR, width=40)
Jobposentry.grid(row=4, column=1, padx=10, pady=20)
name=Label(HR, text="Name")
name.grid(row=1, column=2, padx=10, pady=2, sticky=W)
nameentry=Entry(HR, width=40)
nameentry.grid(row=1, column=3, padx=10, pady=20)
Age=Label(HR, text="Age")
Age.grid(row=2, column=2, padx=10, pady=20, sticky=W)
Ageentry=Entry(HR, width=40)
Ageentry.grid(row=2, column=3, padx=10, pady=20)
NatID=Label(HR, text="National ID")
NatID.grid(row=3, column=2, padx=10, pady=2, sticky=W)
NatIDentry=Entry(frame, width=40)
NatIDentry.grid(row=3, column=3, padx=10, pady=20)
Jobpos=Label(HR, text="Job Position")
Jobpos.grid(row=4, column=2, padx=10, pady=20, sticky=W)
Jobposentry=Entry(HR, width=40)
Jobposentry.grid(row=4, column=3, padx=10, pady=20)
name=Label(HR, text="Name")
name.grid(row=1, column=4, padx=10, pady=2, sticky=W)
nameentry=Entry(HR, width=40)
nameentry.grid(row=1, column=5, padx=10, pady=20)
Age=Label(HR, text="Age")
Age.grid(row=2, column=4, padx=10, pady=20, sticky=W)
Ageentry=Entry(HR, width=40)
Ageentry.grid(row=2, column=5, padx=10, pady=20)
NatID=Label(HR, text="National ID")
NatID.grid(row=3, column=4, padx=10, pady=2, sticky=W)
NatIDentry=Entry(HR, width=40)
NatIDentry.grid(row=3, column=5, padx=10, pady=20)
Jobpos=Label(HR, text="Job Position")
Jobpos.grid(row=4, column=4, padx=10, pady=20, sticky=W)
Jobposentry=Entry(HR, width=40)
Jobposentry.grid(row=4, column=5, padx=10, pady=20)
# content in schain window page
email=Label(Schain, text="email")
email.grid(row=1, column=0, pady=10)
# more frames
# destroy the program
root.mainloop()
解决方案
由于您在这里多次销毁和创建 windows,因此面向对象的方法会有很大帮助。将所有菜单和内容包装到 HRWindow
class 和 SchainWindow
class.
中
HRWindow class:
class HRWindow(tk.Toplevel):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.add_menus()
self.add_content()
def add_menus(self):
# menus for the HR DPT
menubar = tk.Menu(self)
File = tk.Menu(menubar, tearoff=0)
File.add_command(label='Open File')
File.add_command(label='Save')
File.add_command(label='Exit')
menubar.add_cascade(label="File", menu=File)
Departments = tk.Menu(menubar, tearoff=0)
Departments.add_command(label='Human Resource Department',command=HR_change)
Departments.add_command(label='Supply chain Department',command=Schain_change)
menubar.add_cascade(label="Departments", menu=Departments)
Help = tk.Menu(menubar, tearoff=0)
Help.add_command(label='About ihms')
Help.add_command(label='Manual')
menubar.add_cascade(label="Help", menu=Help)
Settings = tk.Menu(menubar, tearoff=0)
Settings.add_command(label='Log Out')
menubar.add_cascade(label="Settings", menu=Settings)
#configure menu for root
self.config(menu=menubar)
def add_content(self):
# content in self window pages
name = tk.Label(self, text="Name")
name.grid(row=1, column=0, padx=10, pady=2, sticky=W)
nameentry = tk.Entry(self, width=40)
nameentry.grid(row=1, column=1, padx=10, pady=20)
Age = tk.Label(self, text="Age")
Age.grid(row=2, column=0, padx=10, pady=20, sticky=W)
Ageentry = tk.Entry(self, width=40)
Ageentry.grid(row=2, column=1, padx=10, pady=20)
NatID = tk.Label(self, text="National ID")
NatID.grid(row=3, column=0, padx=10, pady=2, sticky=W)
NatIDentry = tk.Entry(self, width=40)
NatIDentry.grid(row=3, column=1, padx=10, pady=20)
Jobpos = tk.Label(self, text="Job Position")
Jobpos.grid(row=4, column=0, padx=10, pady=20, sticky=W)
Jobposentry = tk.Entry(self, width=40)
Jobposentry.grid(row=4, column=1, padx=10, pady=20)
name = tk.Label(self, text="Name")
name.grid(row=1, column=2, padx=10, pady=2, sticky=W)
nameentry = tk.Entry(self, width=40)
nameentry.grid(row=1, column=3, padx=10, pady=20)
Age = tk.Label(self, text="Age")
Age.grid(row=2, column=2, padx=10, pady=20, sticky=W)
Ageentry = tk.Entry(self, width=40)
Ageentry.grid(row=2, column=3, padx=10, pady=20)
NatID = tk.Label(self, text="National ID")
NatID.grid(row=3, column=2, padx=10, pady=2, sticky=W)
NatIDentry = tk.Entry(self, width=40)
NatIDentry.grid(row=3, column=3, padx=10, pady=20)
Jobpos = tk.Label(self, text="Job Position")
Jobpos.grid(row=4, column=2, padx=10, pady=20, sticky=W)
Jobposentry = tk.Entry(self, width=40)
Jobposentry.grid(row=4, column=3, padx=10, pady=20)
name = tk.Label(self, text="Name")
name.grid(row=1, column=4, padx=10, pady=2, sticky=W)
nameentry = tk.Entry(self, width=40)
nameentry.grid(row=1, column=5, padx=10, pady=20)
Age = tk.Label(self, text="Age")
Age.grid(row=2, column=4, padx=10, pady=20, sticky=W)
Ageentry = tk.Entry(self, width=40)
Ageentry.grid(row=2, column=5, padx=10, pady=20)
NatID = tk.Label(self, text="National ID")
NatID.grid(row=3, column=4, padx=10, pady=2, sticky=W)
NatIDentry = tk.Entry(self, width=40)
NatIDentry.grid(row=3, column=5, padx=10, pady=20)
Jobpos = tk.Label(self, text="Job Position")
Jobpos.grid(row=4, column=4, padx=10, pady=20, sticky=W)
Jobposentry = tk.Entry(self, width=40)
Jobposentry.grid(row=4, column=5, padx=10, pady=20)
SchainWindow class:
class SchainWindow(tk.Toplevel):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.add_content()
def add_content(self):
email = tk.Label(self, text="email")
email.grid(row=1, column=0, pady=10)
为了确保所有 windows 都处于活动状态,您需要设置一些标志,然后在您的函数中添加对这些标志的检查
schain_active = False
hr_active = False
def HR_change():
global HR, schain_active, root_active
HR = HRWindow()
HR.geometry("1200x900")
HR.title("HR")
if schain_active:
Schain.destroy()
schain_active = False
def Schain_change():
global Schain, hr_active, root_active
Schain = SchainWindow()
Schain.geometry("1200x900")
Schain.title("SUPPLY CHAIN")
if hr_active:
HR.destroy()
hr_active = False
编辑
这可能不是一个优化好的解决方案,我认为您可能想尝试 showing/hiding 帧。
销毁 root
意味着销毁您的应用程序,一旦您销毁 root,您也将失去对其他 Toplevel windows 的访问权限。因此,为了做到这一点,您可以将 Toplevel windows 转换为 tk.Tk
。还创建一个标志来检查根是否可见。以下是您的代码在进行所有更改后的外观:
# try not to import globally
import tkinter as tk
from tkinter.constants import *
root = tk.Tk()
root.geometry("1200x900")
root.title("Hospital Management System")
schain_active = False
hr_active = False
root_active = True
# menus for the HR DPT
class HRWindow(tk.Tk):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.add_menus()
self.add_content()
def add_menus(self):
# menus for the HR DPT
menubar = tk.Menu(self)
File = tk.Menu(menubar, tearoff=0)
File.add_command(label='Open File')
File.add_command(label='Save')
File.add_command(label='Exit')
menubar.add_cascade(label="File", menu=File)
Departments = tk.Menu(menubar, tearoff=0)
Departments.add_command(label='Human Resource Department',command=HR_change)
Departments.add_command(label='Supply chain Department',command=Schain_change)
menubar.add_cascade(label="Departments", menu=Departments)
Help = tk.Menu(menubar, tearoff=0)
Help.add_command(label='About ihms')
Help.add_command(label='Manual')
menubar.add_cascade(label="Help", menu=Help)
Settings = tk.Menu(menubar, tearoff=0)
Settings.add_command(label='Log Out')
menubar.add_cascade(label="Settings", menu=Settings)
#configure menu for root
self.config(menu=menubar)
def add_content(self):
# content in self window pages
name = tk.Label(self, text="Name")
name.grid(row=1, column=0, padx=10, pady=2, sticky=W)
nameentry = tk.Entry(self, width=40)
nameentry.grid(row=1, column=1, padx=10, pady=20)
Age = tk.Label(self, text="Age")
Age.grid(row=2, column=0, padx=10, pady=20, sticky=W)
Ageentry = tk.Entry(self, width=40)
Ageentry.grid(row=2, column=1, padx=10, pady=20)
NatID = tk.Label(self, text="National ID")
NatID.grid(row=3, column=0, padx=10, pady=2, sticky=W)
NatIDentry = tk.Entry(self, width=40)
NatIDentry.grid(row=3, column=1, padx=10, pady=20)
Jobpos = tk.Label(self, text="Job Position")
Jobpos.grid(row=4, column=0, padx=10, pady=20, sticky=W)
Jobposentry = tk.Entry(self, width=40)
Jobposentry.grid(row=4, column=1, padx=10, pady=20)
name = tk.Label(self, text="Name")
name.grid(row=1, column=2, padx=10, pady=2, sticky=W)
nameentry = tk.Entry(self, width=40)
nameentry.grid(row=1, column=3, padx=10, pady=20)
Age = tk.Label(self, text="Age")
Age.grid(row=2, column=2, padx=10, pady=20, sticky=W)
Ageentry = tk.Entry(self, width=40)
Ageentry.grid(row=2, column=3, padx=10, pady=20)
NatID = tk.Label(self, text="National ID")
NatID.grid(row=3, column=2, padx=10, pady=2, sticky=W)
NatIDentry = tk.Entry(self, width=40)
NatIDentry.grid(row=3, column=3, padx=10, pady=20)
Jobpos = tk.Label(self, text="Job Position")
Jobpos.grid(row=4, column=2, padx=10, pady=20, sticky=W)
Jobposentry = tk.Entry(self, width=40)
Jobposentry.grid(row=4, column=3, padx=10, pady=20)
name = tk.Label(self, text="Name")
name.grid(row=1, column=4, padx=10, pady=2, sticky=W)
nameentry = tk.Entry(self, width=40)
nameentry.grid(row=1, column=5, padx=10, pady=20)
Age = tk.Label(self, text="Age")
Age.grid(row=2, column=4, padx=10, pady=20, sticky=W)
Ageentry = tk.Entry(self, width=40)
Ageentry.grid(row=2, column=5, padx=10, pady=20)
NatID = tk.Label(self, text="National ID")
NatID.grid(row=3, column=4, padx=10, pady=2, sticky=W)
NatIDentry = tk.Entry(self, width=40)
NatIDentry.grid(row=3, column=5, padx=10, pady=20)
Jobpos = tk.Label(self, text="Job Position")
Jobpos.grid(row=4, column=4, padx=10, pady=20, sticky=W)
Jobposentry = tk.Entry(self, width=40)
Jobposentry.grid(row=4, column=5, padx=10, pady=20)
class SchainWindow(tk.Tk):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.add_content()
def add_content(self):
email = tk.Label(self, text="email")
email.grid(row=1, column=0, pady=10)
def HR_change():
global HR, schain_active, hr_active, root_active
HR = HRWindow()
HR.geometry("1200x900")
HR.title("HR")
hr_active = True
if root_active:
root.destroy()
root_active = False
if schain_active:
Schain.destroy()
schain_active = False
def Schain_change():
global Schain, hr_active, schain_active, root_active
Schain = SchainWindow()
Schain.geometry("1200x900")
Schain.title("SUPPLY CHAIN")
schain_active = True
if root_active:
root.destroy()
root_active = False
if hr_active:
HR.destroy()
hr_active = False
# menus for the root
menubar = tk.Menu(root)
File = tk.Menu(menubar, tearoff=0)
File.add_command(label='Open File')
File.add_command(label='Save')
File.add_command(label='Exit')
menubar.add_cascade(label="File", menu=File)
Departments = tk.Menu(menubar, tearoff=0)
Departments.add_command(label='Human Resource Department',command=HR_change)
Departments.add_command(label='Supply chain Department',command=Schain_change)
menubar.add_cascade(label="Departments", menu=Departments)
Help = tk.Menu(menubar, tearoff=0)
Help.add_command(label='About ihms')
Help.add_command(label='Manual')
menubar.add_cascade(label="Help", menu=Help)
Settings = tk.Menu(menubar, tearoff=0)
Settings.add_command(label='Log Out')
menubar.add_cascade(label="Settings", menu=Settings)
#configure menu for root
root.config(menu=menubar)
# content in root window pages
name = tk.Label(root, text="Name")
name.grid(row=1, column=0, padx=10, pady=2, sticky=W)
nameentry = tk.Entry(root, width=40)
nameentry.grid(row=1, column=1, padx=10, pady=20)
Age = tk.Label(root, text="Age")
Age.grid(row=2, column=0, padx=10, pady=20, sticky=W)
Ageentry = tk.Entry(root, width=40)
Ageentry.grid(row=2, column=1, padx=10, pady=20)
NatID = tk.Label(root, text="National ID")
NatID.grid(row=3, column=0, padx=10, pady=2, sticky=W)
NatIDentry = tk.Entry(root, width=40)
NatIDentry.grid(row=3, column=1, padx=10, pady=20)
Jobpos = tk.Label(root, text="Job Position")
Jobpos.grid(row=4, column=0, padx=10, pady=20, sticky=W)
Jobposentry = tk.Entry(root, width=40)
Jobposentry.grid(row=4, column=1, padx=10, pady=20)
# destroy the program
root.mainloop()
完整代码
# try not to import globally
import tkinter as tk
from tkinter.constants import *
root = tk.Tk()
root.geometry("1200x900")
root.title("Hospital Management System")
schain_active = False
hr_active = False
# menus for the HR DPT
class HRWindow(tk.Toplevel):
def __init__(self, master, *args, **kwargs):
super().__init__(master, *args, **kwargs)
self.master = master
self.add_menus()
self.add_content()
def add_menus(self):
# menus for the HR DPT
menubar = tk.Menu(self)
File = tk.Menu(menubar, tearoff=0)
File.add_command(label='Open File')
File.add_command(label='Save')
File.add_command(label='Exit')
menubar.add_cascade(label="File", menu=File)
Departments = tk.Menu(menubar, tearoff=0)
Departments.add_command(label='Human Resource Department',command=HR_change)
Departments.add_command(label='Supply chain Department',command=Schain_change)
menubar.add_cascade(label="Departments", menu=Departments)
Help = tk.Menu(menubar, tearoff=0)
Help.add_command(label='About ihms')
Help.add_command(label='Manual')
menubar.add_cascade(label="Help", menu=Help)
Settings = tk.Menu(menubar, tearoff=0)
Settings.add_command(label='Log Out')
menubar.add_cascade(label="Settings", menu=Settings)
#configure menu for root
self.config(menu=menubar)
def add_content(self):
# content in self window pages
name = tk.Label(self, text="Name")
name.grid(row=1, column=0, padx=10, pady=2, sticky=W)
nameentry = tk.Entry(self, width=40)
nameentry.grid(row=1, column=1, padx=10, pady=20)
Age = tk.Label(self, text="Age")
Age.grid(row=2, column=0, padx=10, pady=20, sticky=W)
Ageentry = tk.Entry(self, width=40)
Ageentry.grid(row=2, column=1, padx=10, pady=20)
NatID = tk.Label(self, text="National ID")
NatID.grid(row=3, column=0, padx=10, pady=2, sticky=W)
NatIDentry = tk.Entry(self, width=40)
NatIDentry.grid(row=3, column=1, padx=10, pady=20)
Jobpos = tk.Label(self, text="Job Position")
Jobpos.grid(row=4, column=0, padx=10, pady=20, sticky=W)
Jobposentry = tk.Entry(self, width=40)
Jobposentry.grid(row=4, column=1, padx=10, pady=20)
name = tk.Label(self, text="Name")
name.grid(row=1, column=2, padx=10, pady=2, sticky=W)
nameentry = tk.Entry(self, width=40)
nameentry.grid(row=1, column=3, padx=10, pady=20)
Age = tk.Label(self, text="Age")
Age.grid(row=2, column=2, padx=10, pady=20, sticky=W)
Ageentry = tk.Entry(self, width=40)
Ageentry.grid(row=2, column=3, padx=10, pady=20)
NatID = tk.Label(self, text="National ID")
NatID.grid(row=3, column=2, padx=10, pady=2, sticky=W)
NatIDentry = tk.Entry(self, width=40)
NatIDentry.grid(row=3, column=3, padx=10, pady=20)
Jobpos = tk.Label(self, text="Job Position")
Jobpos.grid(row=4, column=2, padx=10, pady=20, sticky=W)
Jobposentry = tk.Entry(self, width=40)
Jobposentry.grid(row=4, column=3, padx=10, pady=20)
name = tk.Label(self, text="Name")
name.grid(row=1, column=4, padx=10, pady=2, sticky=W)
nameentry = tk.Entry(self, width=40)
nameentry.grid(row=1, column=5, padx=10, pady=20)
Age = tk.Label(self, text="Age")
Age.grid(row=2, column=4, padx=10, pady=20, sticky=W)
Ageentry = tk.Entry(self, width=40)
Ageentry.grid(row=2, column=5, padx=10, pady=20)
NatID = tk.Label(self, text="National ID")
NatID.grid(row=3, column=4, padx=10, pady=2, sticky=W)
NatIDentry = tk.Entry(self, width=40)
NatIDentry.grid(row=3, column=5, padx=10, pady=20)
Jobpos = tk.Label(self, text="Job Position")
Jobpos.grid(row=4, column=4, padx=10, pady=20, sticky=W)
Jobposentry = tk.Entry(self, width=40)
Jobposentry.grid(row=4, column=5, padx=10, pady=20)
class SchainWindow(tk.Toplevel):
def __init__(self, master, *args, **kwargs):
super().__init__(master, *args, **kwargs)
self.master = master
self.add_content()
def add_content(self):
email = tk.Label(self, text="email")
email.grid(row=1, column=0, pady=10)
def HR_change():
global HR, schain_active
HR = HRWindow(root)
HR.geometry("1200x900")
HR.title("HR")
if schain_active:
Schain.destroy()
schain_active = False
def Schain_change():
global Schain, hr_active
Schain = SchainWindow(root)
Schain.geometry("1200x900")
Schain.title("SUPPLY CHAIN")
if hr_active:
HR.destroy()
hr_active = False
# menus for the root
menubar = tk.Menu(root)
File = tk.Menu(menubar, tearoff=0)
File.add_command(label='Open File')
File.add_command(label='Save')
File.add_command(label='Exit')
menubar.add_cascade(label="File", menu=File)
Departments = tk.Menu(menubar, tearoff=0)
Departments.add_command(label='Human Resource Department',command=HR_change)
Departments.add_command(label='Supply chain Department',command=Schain_change)
menubar.add_cascade(label="Departments", menu=Departments)
Help = tk.Menu(menubar, tearoff=0)
Help.add_command(label='About ihms')
Help.add_command(label='Manual')
menubar.add_cascade(label="Help", menu=Help)
Settings = tk.Menu(menubar, tearoff=0)
Settings.add_command(label='Log Out')
menubar.add_cascade(label="Settings", menu=Settings)
#configure menu for root
root.config(menu=menubar)
# content in root window pages
name = tk.Label(root, text="Name")
name.grid(row=1, column=0, padx=10, pady=2, sticky=W)
nameentry = tk.Entry(root, width=40)
nameentry.grid(row=1, column=1, padx=10, pady=20)
Age = tk.Label(root, text="Age")
Age.grid(row=2, column=0, padx=10, pady=20, sticky=W)
Ageentry = tk.Entry(root, width=40)
Ageentry.grid(row=2, column=1, padx=10, pady=20)
NatID = tk.Label(root, text="National ID")
NatID.grid(row=3, column=0, padx=10, pady=2, sticky=W)
NatIDentry = tk.Entry(root, width=40)
NatIDentry.grid(row=3, column=1, padx=10, pady=20)
Jobpos = tk.Label(root, text="Job Position")
Jobpos.grid(row=4, column=0, padx=10, pady=20, sticky=W)
Jobposentry = tk.Entry(root, width=40)
Jobposentry.grid(row=4, column=1, padx=10, pady=20)
# destroy the program
root.mainloop()
我正在尝试创建一个 Tkinter 管理系统,其页面可通过菜单打开。打开页面时,我想销毁上一页并打开一个新页面(所有这些都通过菜单按钮),但我没有成功。请问,有人可以帮我编辑代码吗?我会很感激的。我一直在努力研究代码,直到我感到困惑。
from tkinter import *
root= Tk()
root.geometry("1200x900")
root.title("Hospital Management System")
root.iconbitmap(r'C:\Users\KEN\electron-api-demos\development\grad.ico')
#=========================functions for switching the windows========
def HR_change():
HR=Tk()
HR.geometry("1200x900")
HR.title("HR")
HR.grid()
Schain.destroy()
root.destroy()
def Schain_change():
Schain=Tk()
Schain.geometry("1200x900")
Schain.title("SUPPLY CHAIN")
Schain.grid()
HR.destroy()
root.destroy()
# menus for the root
menubar= Menu(root)
File=Menu(menubar, tearoff=0)
File.add_command(label='Open File')
File.add_command(label='Save')
File.add_command(label='Exit')
menubar.add_cascade(label="File", menu=File)
Departments=Menu(menubar, tearoff=0)
Departments.add_command(label='Human Resource Department',command=HR_change)
Departments.add_command(label='Supply chain Department',command=Schain_change)
menubar.add_cascade(label="Departments", menu=Departments)
Help=Menu(menubar, tearoff=0)
Help.add_command(label='About ihms')
Help.add_command(label='Manual')
menubar.add_cascade(label="Help", menu=Help)
Settings=Menu(menubar, tearoff=0)
Settings.add_command(label='Log Out')
menubar.add_cascade(label="Settings", menu=Settings)
#configure menu for root
root.config(menu=menubar)
# menus for the HR DPT
menubar= Menu(HR)
File=Menu(menubar, tearoff=0)
File.add_command(label='Open File')
File.add_command(label='Save')
File.add_command(label='Exit')
menubar.add_cascade(label="File", menu=File)
Departments=Menu(menubar, tearoff=0)
Departments.add_command(label='Human Resource Department',command=HR_change)
Departments.add_command(label='Supply chain Department',command=Schain_change)
menubar.add_cascade(label="Departments", menu=Departments)
Help=Menu(menubar, tearoff=0)
Help.add_command(label='About ihms')
Help.add_command(label='Manual')
menubar.add_cascade(label="Help", menu=Help)
Settings=Menu(menubar, tearoff=0)
Settings.add_command(label='Log Out')
menubar.add_cascade(label="Settings", menu=Settings)
#configure menu for root
HR.config(menu=menubar)
# content in HR window pages
name=Label(HR, text="Name")
name.grid(row=1, column=0, padx=10, pady=2, sticky=W)
nameentry=Entry(HR, width=40)
nameentry.grid(row=1, column=1, padx=10, pady=20)
Age=Label(HR, text="Age")
Age.grid(row=2, column=0, padx=10, pady=20, sticky=W)
Ageentry=Entry(HR, width=40)
Ageentry.grid(row=2, column=1, padx=10, pady=20)
NatID=Label(HR, text="National ID")
NatID.grid(row=3, column=0, padx=10, pady=2, sticky=W)
NatIDentry=Entry(HR, width=40)
NatIDentry.grid(row=3, column=1, padx=10, pady=20)
Jobpos=Label(HR, text="Job Position")
Jobpos.grid(row=4, column=0, padx=10, pady=20, sticky=W)
Jobposentry=Entry(HR, width=40)
Jobposentry.grid(row=4, column=1, padx=10, pady=20)
name=Label(HR, text="Name")
name.grid(row=1, column=2, padx=10, pady=2, sticky=W)
nameentry=Entry(HR, width=40)
nameentry.grid(row=1, column=3, padx=10, pady=20)
Age=Label(HR, text="Age")
Age.grid(row=2, column=2, padx=10, pady=20, sticky=W)
Ageentry=Entry(HR, width=40)
Ageentry.grid(row=2, column=3, padx=10, pady=20)
NatID=Label(HR, text="National ID")
NatID.grid(row=3, column=2, padx=10, pady=2, sticky=W)
NatIDentry=Entry(frame, width=40)
NatIDentry.grid(row=3, column=3, padx=10, pady=20)
Jobpos=Label(HR, text="Job Position")
Jobpos.grid(row=4, column=2, padx=10, pady=20, sticky=W)
Jobposentry=Entry(HR, width=40)
Jobposentry.grid(row=4, column=3, padx=10, pady=20)
name=Label(HR, text="Name")
name.grid(row=1, column=4, padx=10, pady=2, sticky=W)
nameentry=Entry(HR, width=40)
nameentry.grid(row=1, column=5, padx=10, pady=20)
Age=Label(HR, text="Age")
Age.grid(row=2, column=4, padx=10, pady=20, sticky=W)
Ageentry=Entry(HR, width=40)
Ageentry.grid(row=2, column=5, padx=10, pady=20)
NatID=Label(HR, text="National ID")
NatID.grid(row=3, column=4, padx=10, pady=2, sticky=W)
NatIDentry=Entry(HR, width=40)
NatIDentry.grid(row=3, column=5, padx=10, pady=20)
Jobpos=Label(HR, text="Job Position")
Jobpos.grid(row=4, column=4, padx=10, pady=20, sticky=W)
Jobposentry=Entry(HR, width=40)
Jobposentry.grid(row=4, column=5, padx=10, pady=20)
# content in schain window page
email=Label(Schain, text="email")
email.grid(row=1, column=0, pady=10)
# more frames
# destroy the program
root.mainloop()
解决方案
由于您在这里多次销毁和创建 windows,因此面向对象的方法会有很大帮助。将所有菜单和内容包装到 HRWindow
class 和 SchainWindow
class.
HRWindow class:
class HRWindow(tk.Toplevel):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.add_menus()
self.add_content()
def add_menus(self):
# menus for the HR DPT
menubar = tk.Menu(self)
File = tk.Menu(menubar, tearoff=0)
File.add_command(label='Open File')
File.add_command(label='Save')
File.add_command(label='Exit')
menubar.add_cascade(label="File", menu=File)
Departments = tk.Menu(menubar, tearoff=0)
Departments.add_command(label='Human Resource Department',command=HR_change)
Departments.add_command(label='Supply chain Department',command=Schain_change)
menubar.add_cascade(label="Departments", menu=Departments)
Help = tk.Menu(menubar, tearoff=0)
Help.add_command(label='About ihms')
Help.add_command(label='Manual')
menubar.add_cascade(label="Help", menu=Help)
Settings = tk.Menu(menubar, tearoff=0)
Settings.add_command(label='Log Out')
menubar.add_cascade(label="Settings", menu=Settings)
#configure menu for root
self.config(menu=menubar)
def add_content(self):
# content in self window pages
name = tk.Label(self, text="Name")
name.grid(row=1, column=0, padx=10, pady=2, sticky=W)
nameentry = tk.Entry(self, width=40)
nameentry.grid(row=1, column=1, padx=10, pady=20)
Age = tk.Label(self, text="Age")
Age.grid(row=2, column=0, padx=10, pady=20, sticky=W)
Ageentry = tk.Entry(self, width=40)
Ageentry.grid(row=2, column=1, padx=10, pady=20)
NatID = tk.Label(self, text="National ID")
NatID.grid(row=3, column=0, padx=10, pady=2, sticky=W)
NatIDentry = tk.Entry(self, width=40)
NatIDentry.grid(row=3, column=1, padx=10, pady=20)
Jobpos = tk.Label(self, text="Job Position")
Jobpos.grid(row=4, column=0, padx=10, pady=20, sticky=W)
Jobposentry = tk.Entry(self, width=40)
Jobposentry.grid(row=4, column=1, padx=10, pady=20)
name = tk.Label(self, text="Name")
name.grid(row=1, column=2, padx=10, pady=2, sticky=W)
nameentry = tk.Entry(self, width=40)
nameentry.grid(row=1, column=3, padx=10, pady=20)
Age = tk.Label(self, text="Age")
Age.grid(row=2, column=2, padx=10, pady=20, sticky=W)
Ageentry = tk.Entry(self, width=40)
Ageentry.grid(row=2, column=3, padx=10, pady=20)
NatID = tk.Label(self, text="National ID")
NatID.grid(row=3, column=2, padx=10, pady=2, sticky=W)
NatIDentry = tk.Entry(self, width=40)
NatIDentry.grid(row=3, column=3, padx=10, pady=20)
Jobpos = tk.Label(self, text="Job Position")
Jobpos.grid(row=4, column=2, padx=10, pady=20, sticky=W)
Jobposentry = tk.Entry(self, width=40)
Jobposentry.grid(row=4, column=3, padx=10, pady=20)
name = tk.Label(self, text="Name")
name.grid(row=1, column=4, padx=10, pady=2, sticky=W)
nameentry = tk.Entry(self, width=40)
nameentry.grid(row=1, column=5, padx=10, pady=20)
Age = tk.Label(self, text="Age")
Age.grid(row=2, column=4, padx=10, pady=20, sticky=W)
Ageentry = tk.Entry(self, width=40)
Ageentry.grid(row=2, column=5, padx=10, pady=20)
NatID = tk.Label(self, text="National ID")
NatID.grid(row=3, column=4, padx=10, pady=2, sticky=W)
NatIDentry = tk.Entry(self, width=40)
NatIDentry.grid(row=3, column=5, padx=10, pady=20)
Jobpos = tk.Label(self, text="Job Position")
Jobpos.grid(row=4, column=4, padx=10, pady=20, sticky=W)
Jobposentry = tk.Entry(self, width=40)
Jobposentry.grid(row=4, column=5, padx=10, pady=20)
SchainWindow class:
class SchainWindow(tk.Toplevel):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.add_content()
def add_content(self):
email = tk.Label(self, text="email")
email.grid(row=1, column=0, pady=10)
为了确保所有 windows 都处于活动状态,您需要设置一些标志,然后在您的函数中添加对这些标志的检查
schain_active = False
hr_active = False
def HR_change():
global HR, schain_active, root_active
HR = HRWindow()
HR.geometry("1200x900")
HR.title("HR")
if schain_active:
Schain.destroy()
schain_active = False
def Schain_change():
global Schain, hr_active, root_active
Schain = SchainWindow()
Schain.geometry("1200x900")
Schain.title("SUPPLY CHAIN")
if hr_active:
HR.destroy()
hr_active = False
编辑
这可能不是一个优化好的解决方案,我认为您可能想尝试 showing/hiding 帧。
销毁 root
意味着销毁您的应用程序,一旦您销毁 root,您也将失去对其他 Toplevel windows 的访问权限。因此,为了做到这一点,您可以将 Toplevel windows 转换为 tk.Tk
。还创建一个标志来检查根是否可见。以下是您的代码在进行所有更改后的外观:
# try not to import globally
import tkinter as tk
from tkinter.constants import *
root = tk.Tk()
root.geometry("1200x900")
root.title("Hospital Management System")
schain_active = False
hr_active = False
root_active = True
# menus for the HR DPT
class HRWindow(tk.Tk):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.add_menus()
self.add_content()
def add_menus(self):
# menus for the HR DPT
menubar = tk.Menu(self)
File = tk.Menu(menubar, tearoff=0)
File.add_command(label='Open File')
File.add_command(label='Save')
File.add_command(label='Exit')
menubar.add_cascade(label="File", menu=File)
Departments = tk.Menu(menubar, tearoff=0)
Departments.add_command(label='Human Resource Department',command=HR_change)
Departments.add_command(label='Supply chain Department',command=Schain_change)
menubar.add_cascade(label="Departments", menu=Departments)
Help = tk.Menu(menubar, tearoff=0)
Help.add_command(label='About ihms')
Help.add_command(label='Manual')
menubar.add_cascade(label="Help", menu=Help)
Settings = tk.Menu(menubar, tearoff=0)
Settings.add_command(label='Log Out')
menubar.add_cascade(label="Settings", menu=Settings)
#configure menu for root
self.config(menu=menubar)
def add_content(self):
# content in self window pages
name = tk.Label(self, text="Name")
name.grid(row=1, column=0, padx=10, pady=2, sticky=W)
nameentry = tk.Entry(self, width=40)
nameentry.grid(row=1, column=1, padx=10, pady=20)
Age = tk.Label(self, text="Age")
Age.grid(row=2, column=0, padx=10, pady=20, sticky=W)
Ageentry = tk.Entry(self, width=40)
Ageentry.grid(row=2, column=1, padx=10, pady=20)
NatID = tk.Label(self, text="National ID")
NatID.grid(row=3, column=0, padx=10, pady=2, sticky=W)
NatIDentry = tk.Entry(self, width=40)
NatIDentry.grid(row=3, column=1, padx=10, pady=20)
Jobpos = tk.Label(self, text="Job Position")
Jobpos.grid(row=4, column=0, padx=10, pady=20, sticky=W)
Jobposentry = tk.Entry(self, width=40)
Jobposentry.grid(row=4, column=1, padx=10, pady=20)
name = tk.Label(self, text="Name")
name.grid(row=1, column=2, padx=10, pady=2, sticky=W)
nameentry = tk.Entry(self, width=40)
nameentry.grid(row=1, column=3, padx=10, pady=20)
Age = tk.Label(self, text="Age")
Age.grid(row=2, column=2, padx=10, pady=20, sticky=W)
Ageentry = tk.Entry(self, width=40)
Ageentry.grid(row=2, column=3, padx=10, pady=20)
NatID = tk.Label(self, text="National ID")
NatID.grid(row=3, column=2, padx=10, pady=2, sticky=W)
NatIDentry = tk.Entry(self, width=40)
NatIDentry.grid(row=3, column=3, padx=10, pady=20)
Jobpos = tk.Label(self, text="Job Position")
Jobpos.grid(row=4, column=2, padx=10, pady=20, sticky=W)
Jobposentry = tk.Entry(self, width=40)
Jobposentry.grid(row=4, column=3, padx=10, pady=20)
name = tk.Label(self, text="Name")
name.grid(row=1, column=4, padx=10, pady=2, sticky=W)
nameentry = tk.Entry(self, width=40)
nameentry.grid(row=1, column=5, padx=10, pady=20)
Age = tk.Label(self, text="Age")
Age.grid(row=2, column=4, padx=10, pady=20, sticky=W)
Ageentry = tk.Entry(self, width=40)
Ageentry.grid(row=2, column=5, padx=10, pady=20)
NatID = tk.Label(self, text="National ID")
NatID.grid(row=3, column=4, padx=10, pady=2, sticky=W)
NatIDentry = tk.Entry(self, width=40)
NatIDentry.grid(row=3, column=5, padx=10, pady=20)
Jobpos = tk.Label(self, text="Job Position")
Jobpos.grid(row=4, column=4, padx=10, pady=20, sticky=W)
Jobposentry = tk.Entry(self, width=40)
Jobposentry.grid(row=4, column=5, padx=10, pady=20)
class SchainWindow(tk.Tk):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.add_content()
def add_content(self):
email = tk.Label(self, text="email")
email.grid(row=1, column=0, pady=10)
def HR_change():
global HR, schain_active, hr_active, root_active
HR = HRWindow()
HR.geometry("1200x900")
HR.title("HR")
hr_active = True
if root_active:
root.destroy()
root_active = False
if schain_active:
Schain.destroy()
schain_active = False
def Schain_change():
global Schain, hr_active, schain_active, root_active
Schain = SchainWindow()
Schain.geometry("1200x900")
Schain.title("SUPPLY CHAIN")
schain_active = True
if root_active:
root.destroy()
root_active = False
if hr_active:
HR.destroy()
hr_active = False
# menus for the root
menubar = tk.Menu(root)
File = tk.Menu(menubar, tearoff=0)
File.add_command(label='Open File')
File.add_command(label='Save')
File.add_command(label='Exit')
menubar.add_cascade(label="File", menu=File)
Departments = tk.Menu(menubar, tearoff=0)
Departments.add_command(label='Human Resource Department',command=HR_change)
Departments.add_command(label='Supply chain Department',command=Schain_change)
menubar.add_cascade(label="Departments", menu=Departments)
Help = tk.Menu(menubar, tearoff=0)
Help.add_command(label='About ihms')
Help.add_command(label='Manual')
menubar.add_cascade(label="Help", menu=Help)
Settings = tk.Menu(menubar, tearoff=0)
Settings.add_command(label='Log Out')
menubar.add_cascade(label="Settings", menu=Settings)
#configure menu for root
root.config(menu=menubar)
# content in root window pages
name = tk.Label(root, text="Name")
name.grid(row=1, column=0, padx=10, pady=2, sticky=W)
nameentry = tk.Entry(root, width=40)
nameentry.grid(row=1, column=1, padx=10, pady=20)
Age = tk.Label(root, text="Age")
Age.grid(row=2, column=0, padx=10, pady=20, sticky=W)
Ageentry = tk.Entry(root, width=40)
Ageentry.grid(row=2, column=1, padx=10, pady=20)
NatID = tk.Label(root, text="National ID")
NatID.grid(row=3, column=0, padx=10, pady=2, sticky=W)
NatIDentry = tk.Entry(root, width=40)
NatIDentry.grid(row=3, column=1, padx=10, pady=20)
Jobpos = tk.Label(root, text="Job Position")
Jobpos.grid(row=4, column=0, padx=10, pady=20, sticky=W)
Jobposentry = tk.Entry(root, width=40)
Jobposentry.grid(row=4, column=1, padx=10, pady=20)
# destroy the program
root.mainloop()
完整代码
# try not to import globally
import tkinter as tk
from tkinter.constants import *
root = tk.Tk()
root.geometry("1200x900")
root.title("Hospital Management System")
schain_active = False
hr_active = False
# menus for the HR DPT
class HRWindow(tk.Toplevel):
def __init__(self, master, *args, **kwargs):
super().__init__(master, *args, **kwargs)
self.master = master
self.add_menus()
self.add_content()
def add_menus(self):
# menus for the HR DPT
menubar = tk.Menu(self)
File = tk.Menu(menubar, tearoff=0)
File.add_command(label='Open File')
File.add_command(label='Save')
File.add_command(label='Exit')
menubar.add_cascade(label="File", menu=File)
Departments = tk.Menu(menubar, tearoff=0)
Departments.add_command(label='Human Resource Department',command=HR_change)
Departments.add_command(label='Supply chain Department',command=Schain_change)
menubar.add_cascade(label="Departments", menu=Departments)
Help = tk.Menu(menubar, tearoff=0)
Help.add_command(label='About ihms')
Help.add_command(label='Manual')
menubar.add_cascade(label="Help", menu=Help)
Settings = tk.Menu(menubar, tearoff=0)
Settings.add_command(label='Log Out')
menubar.add_cascade(label="Settings", menu=Settings)
#configure menu for root
self.config(menu=menubar)
def add_content(self):
# content in self window pages
name = tk.Label(self, text="Name")
name.grid(row=1, column=0, padx=10, pady=2, sticky=W)
nameentry = tk.Entry(self, width=40)
nameentry.grid(row=1, column=1, padx=10, pady=20)
Age = tk.Label(self, text="Age")
Age.grid(row=2, column=0, padx=10, pady=20, sticky=W)
Ageentry = tk.Entry(self, width=40)
Ageentry.grid(row=2, column=1, padx=10, pady=20)
NatID = tk.Label(self, text="National ID")
NatID.grid(row=3, column=0, padx=10, pady=2, sticky=W)
NatIDentry = tk.Entry(self, width=40)
NatIDentry.grid(row=3, column=1, padx=10, pady=20)
Jobpos = tk.Label(self, text="Job Position")
Jobpos.grid(row=4, column=0, padx=10, pady=20, sticky=W)
Jobposentry = tk.Entry(self, width=40)
Jobposentry.grid(row=4, column=1, padx=10, pady=20)
name = tk.Label(self, text="Name")
name.grid(row=1, column=2, padx=10, pady=2, sticky=W)
nameentry = tk.Entry(self, width=40)
nameentry.grid(row=1, column=3, padx=10, pady=20)
Age = tk.Label(self, text="Age")
Age.grid(row=2, column=2, padx=10, pady=20, sticky=W)
Ageentry = tk.Entry(self, width=40)
Ageentry.grid(row=2, column=3, padx=10, pady=20)
NatID = tk.Label(self, text="National ID")
NatID.grid(row=3, column=2, padx=10, pady=2, sticky=W)
NatIDentry = tk.Entry(self, width=40)
NatIDentry.grid(row=3, column=3, padx=10, pady=20)
Jobpos = tk.Label(self, text="Job Position")
Jobpos.grid(row=4, column=2, padx=10, pady=20, sticky=W)
Jobposentry = tk.Entry(self, width=40)
Jobposentry.grid(row=4, column=3, padx=10, pady=20)
name = tk.Label(self, text="Name")
name.grid(row=1, column=4, padx=10, pady=2, sticky=W)
nameentry = tk.Entry(self, width=40)
nameentry.grid(row=1, column=5, padx=10, pady=20)
Age = tk.Label(self, text="Age")
Age.grid(row=2, column=4, padx=10, pady=20, sticky=W)
Ageentry = tk.Entry(self, width=40)
Ageentry.grid(row=2, column=5, padx=10, pady=20)
NatID = tk.Label(self, text="National ID")
NatID.grid(row=3, column=4, padx=10, pady=2, sticky=W)
NatIDentry = tk.Entry(self, width=40)
NatIDentry.grid(row=3, column=5, padx=10, pady=20)
Jobpos = tk.Label(self, text="Job Position")
Jobpos.grid(row=4, column=4, padx=10, pady=20, sticky=W)
Jobposentry = tk.Entry(self, width=40)
Jobposentry.grid(row=4, column=5, padx=10, pady=20)
class SchainWindow(tk.Toplevel):
def __init__(self, master, *args, **kwargs):
super().__init__(master, *args, **kwargs)
self.master = master
self.add_content()
def add_content(self):
email = tk.Label(self, text="email")
email.grid(row=1, column=0, pady=10)
def HR_change():
global HR, schain_active
HR = HRWindow(root)
HR.geometry("1200x900")
HR.title("HR")
if schain_active:
Schain.destroy()
schain_active = False
def Schain_change():
global Schain, hr_active
Schain = SchainWindow(root)
Schain.geometry("1200x900")
Schain.title("SUPPLY CHAIN")
if hr_active:
HR.destroy()
hr_active = False
# menus for the root
menubar = tk.Menu(root)
File = tk.Menu(menubar, tearoff=0)
File.add_command(label='Open File')
File.add_command(label='Save')
File.add_command(label='Exit')
menubar.add_cascade(label="File", menu=File)
Departments = tk.Menu(menubar, tearoff=0)
Departments.add_command(label='Human Resource Department',command=HR_change)
Departments.add_command(label='Supply chain Department',command=Schain_change)
menubar.add_cascade(label="Departments", menu=Departments)
Help = tk.Menu(menubar, tearoff=0)
Help.add_command(label='About ihms')
Help.add_command(label='Manual')
menubar.add_cascade(label="Help", menu=Help)
Settings = tk.Menu(menubar, tearoff=0)
Settings.add_command(label='Log Out')
menubar.add_cascade(label="Settings", menu=Settings)
#configure menu for root
root.config(menu=menubar)
# content in root window pages
name = tk.Label(root, text="Name")
name.grid(row=1, column=0, padx=10, pady=2, sticky=W)
nameentry = tk.Entry(root, width=40)
nameentry.grid(row=1, column=1, padx=10, pady=20)
Age = tk.Label(root, text="Age")
Age.grid(row=2, column=0, padx=10, pady=20, sticky=W)
Ageentry = tk.Entry(root, width=40)
Ageentry.grid(row=2, column=1, padx=10, pady=20)
NatID = tk.Label(root, text="National ID")
NatID.grid(row=3, column=0, padx=10, pady=2, sticky=W)
NatIDentry = tk.Entry(root, width=40)
NatIDentry.grid(row=3, column=1, padx=10, pady=20)
Jobpos = tk.Label(root, text="Job Position")
Jobpos.grid(row=4, column=0, padx=10, pady=20, sticky=W)
Jobposentry = tk.Entry(root, width=40)
Jobposentry.grid(row=4, column=1, padx=10, pady=20)
# destroy the program
root.mainloop()