I am trying to create a Tkinter management system with pages that opens through the menu. While opening a page, I want to destroy the previous page and open a new one(all these through the menu buttons) but I am not succeeding. Kindly, can someone help me edit the code? I will appreciate it. I have struggled with the code until I am getting confused.
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()
CodePudding user response:
Solution
Since you are destroying and creating windows many times here, an object oriented approach will help a lot. Wrap all your menus and content into an HRWindow
class and an 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)
To make sure which all windows are active, you will need to set some flags, and then add checks for these in your functions
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
Edit
This may not be a well optimized solution, you might want to try showing/hiding frames instead in my opinion.
Destroying root
implies destroying your application, you will lose access to other Toplevel windows too, once you destroy root. So in order to do that, you can convert the Toplevel windows into tk.Tk
. Also create a flag for checking whether the root is visible. Here's how your code would look with all that changes made:
# 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()
Complete code
# 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()