I am trying to create frames inside tkinter windows so that I can create label and entry widgets inside the frames but I am not succeeding. I am actually trying to develop a management system. Kindly, can someone help me edit the codes below to create the frames inside the "Schain" and "HR" windows? I will appreciate it.
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
HRmainframe=tk.Frame(HR, width=900, height=800, bd=1, padx=20, pady=20, bg=”red”, relief=RIDGE)
HRmainframe.grid()
Email = tk.Label(HRmainframe, text="Email")
Email.grid(row=1, column=0, padx=10, pady=2, sticky=W)
Emailentry = tk.Entry(HRmainframe, width=40)
Emailentry.grid(row=1, column=1, padx=10, pady=20)
class SchainWindow(tk.Tk):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.add_content()
def add_content(self):
schainmainframe=tk.Frame(Schain, width=900, height=800, bd=1, padx=20, pady=20, bg=”blue”, relief=RIDGE)
schainmainframe.grid()
name = tk.Label(schainmainframe, text="Name")
name.grid(row=1, column=0, padx=10, pady=2, sticky=W)
nameentry = tk.Entry(schainmainframe, width=40)
nameentry.grid(row=1, column=1, padx=10, pady=20)
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)
# destroy the program
root.mainloop()
CodePudding user response:
There are some other issues with the code that I haven't resolved below but this does resolve the original issue of frames not being shown in the children windows.
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.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
HRmainframe=tk.Frame(self, width=900, height=800, bd=1, padx=20, pady=20, bg="red", relief=RIDGE)
HRmainframe.grid()
Email = tk.Label(HRmainframe, text="Email")
Email.grid(row=1, column=0, padx=10, pady=2, sticky=W)
Emailentry = tk.Entry(HRmainframe, width=40)
Emailentry.grid(row=1, column=1, padx=10, pady=20)
class SchainWindow(tk.Toplevel):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.add_content()
def add_content(self):
schainmainframe=tk.Frame(self, width=900, height=800, bd=1, padx=20, pady=20, bg="blue", relief=RIDGE)
schainmainframe.grid()
name = tk.Label(schainmainframe, text="Name")
name.grid(row=1, column=0, padx=10, pady=2, sticky=W)
nameentry = tk.Entry(schainmainframe, width=40)
nameentry.grid(row=1, column=1, padx=10, pady=20)
def HR_change():
global HR, schain_active, hr_active, root_active
HR = HRWindow()
HR.geometry("1200x900")
HR.title("HR")
hr_active = True
def Schain_change():
global Schain, hr_active, schain_active, root_active
Schain = SchainWindow()
Schain.geometry("1200x900")
Schain.title("SUPPLY CHAIN")
schain_active = True
# 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)
# destroy the program
root.mainloop()
I've removed the sections of code that destroy the root window since an application should always have one and only one Tk
instance.
CodePudding user response:
When you create a class that inherits from a widget and you want to put frames inside that object, you need to place them in self
.
Instead of this:
def add_content(self):
schainmainframe=tk.Frame(Schain, ...)
...
It needs to be this:
def add_content(self):
schainmainframe=tk.Frame(self, ...)
...
You need to make a similar change in the other add_content
method.
You also have two root windows which is not how tkinter is designed to be used. Unless you have a fairly deep understanding of tkinter, you should only have a single instance of Tk
. I don't know if that's contributing to your problem, but it will certainly cause behavior that you don't expect.