如何在两个 tkinter 框架之间切换
How to switch between two tkinter frames
我正在尝试使用下面的代码在 tkinter 中切换和显示两个帧,但我没有成功。我附上了代码显示内容的图片。
from tkinter import*
root= Tk()
root.title("HOSPITAL MANAGEMENT SYSTEM")
root.geometry('600x350')
#=======frames====
HR=Frame(root)
Schain=Frame(root)
#=========================functions for switching the frames========
def change_to_HR():
HR.pack(fill='BOTH', expand=1)
Schain.pack_forget()
def change_to_Schain():
Schain.pack(fill='BOTH', expand=1)
HR.pack_forget()
#=====================Add heading logo in the frames======
labelHR=Label(HR, text="HR DEPARTMENT")
labelHR.pack(pady=20)
labelSchain=Label(Schain, text="SUPPLY CHAIN DEPARTMENT")
labelSchain.pack(pady=20)
#===============================add button to switch between frames=====
btn1=Button(root, text="HR", command=change_to_HR)
btn1.pack(pady=20)
btn2=Button(root, text="SUPPLY CHAIN", command=change_to_Schain)
btn2.pack(pady=20)
root.mainloop()
错误在以下几行:
HR.pack(fill='BOTH', expand=1)
Schain.pack(fill='BOTH', expand=1)
您必须直接以小写形式给出字符串 'both'
或使用 pre-defined tkinter 常量 BOTH
。在这两个地方将 'BOTH'
更改为 BOTH
后,您的代码可以正常工作。
工作代码:
from tkinter import *
root= Tk()
root.title("HOSPITAL MANAGEMENT SYSTEM")
root.geometry('600x350')
#=======frames====
HR=Frame(root)
Schain=Frame(root)
#=========================functions for switching the frames========
def change_to_HR():
HR.pack(fill=BOTH, expand=1)
Schain.pack_forget()
def change_to_Schain():
Schain.pack(fill=BOTH, expand=1)
HR.pack_forget()
#=====================Add heading logo in the frames======
labelHR=Label(HR, text="HR DEPARTMENT")
labelHR.pack(pady=20)
labelSchain=Label(Schain, text="SUPPLY CHAIN DEPARTMENT")
labelSchain.pack(pady=20)
#===============================add button to switch between frames=====
btn1=Button(root, text="HR", command=change_to_HR)
btn1.pack(pady=20)
btn2=Button(root, text="SUPPLY CHAIN", command=change_to_Schain)
btn2.pack(pady=20)
root.mainloop()
我正在尝试使用下面的代码在 tkinter 中切换和显示两个帧,但我没有成功。我附上了代码显示内容的图片。
from tkinter import*
root= Tk()
root.title("HOSPITAL MANAGEMENT SYSTEM")
root.geometry('600x350')
#=======frames====
HR=Frame(root)
Schain=Frame(root)
#=========================functions for switching the frames========
def change_to_HR():
HR.pack(fill='BOTH', expand=1)
Schain.pack_forget()
def change_to_Schain():
Schain.pack(fill='BOTH', expand=1)
HR.pack_forget()
#=====================Add heading logo in the frames======
labelHR=Label(HR, text="HR DEPARTMENT")
labelHR.pack(pady=20)
labelSchain=Label(Schain, text="SUPPLY CHAIN DEPARTMENT")
labelSchain.pack(pady=20)
#===============================add button to switch between frames=====
btn1=Button(root, text="HR", command=change_to_HR)
btn1.pack(pady=20)
btn2=Button(root, text="SUPPLY CHAIN", command=change_to_Schain)
btn2.pack(pady=20)
root.mainloop()
错误在以下几行:
HR.pack(fill='BOTH', expand=1)
Schain.pack(fill='BOTH', expand=1)
您必须直接以小写形式给出字符串 'both'
或使用 pre-defined tkinter 常量 BOTH
。在这两个地方将 'BOTH'
更改为 BOTH
后,您的代码可以正常工作。
工作代码:
from tkinter import *
root= Tk()
root.title("HOSPITAL MANAGEMENT SYSTEM")
root.geometry('600x350')
#=======frames====
HR=Frame(root)
Schain=Frame(root)
#=========================functions for switching the frames========
def change_to_HR():
HR.pack(fill=BOTH, expand=1)
Schain.pack_forget()
def change_to_Schain():
Schain.pack(fill=BOTH, expand=1)
HR.pack_forget()
#=====================Add heading logo in the frames======
labelHR=Label(HR, text="HR DEPARTMENT")
labelHR.pack(pady=20)
labelSchain=Label(Schain, text="SUPPLY CHAIN DEPARTMENT")
labelSchain.pack(pady=20)
#===============================add button to switch between frames=====
btn1=Button(root, text="HR", command=change_to_HR)
btn1.pack(pady=20)
btn2=Button(root, text="SUPPLY CHAIN", command=change_to_Schain)
btn2.pack(pady=20)
root.mainloop()