Tkinter 页面未加载
Tkinter page not loading
我的 Tkinter 程序中的一个页面没有加载。我不确定为什么
我的 InsertOperation
class 中有一个 get()
方法,它用于从前一个 class 中检索所选单选按钮的值并执行适当的部分我的代码。然而,这个 get()
方法似乎并没有这样做。有替代方案吗?
class MyOMSApp(Tk):
def __init__(self, *args, **kwargs):
Tk.__init__(self, *args, **kwargs)
container = Frame(self)
container.pack(side="top", fill="both")
container.grid_rowconfigure(0, weight=1)
container.grid_columnconfigure(0, weight=1)
self.frames = {}
for op in (MainPage, InsertMenu, InsertOperation):
my_ops = op(container, self)
self.frames[op] = my_ops
my_ops.grid(row=0, column=0, sticky='nsew')
my_ops.grid(row=550, column=550, sticky='nsew')
self.show_frame(MainPage)
def show_frame(self, cont):
my_ops = self.frames[cont]
my_ops.tkraise()
class MainPage(Frame):
def __init__(self, root, run):
Frame.__init__(self, root)
main_label = Label(self, text="WELCOME TO THE OFFICE MANAGEMENT SYSTEM")
main_label.pack(pady=10, padx=10)
second_label = Label(self, text="Welcome\n\nChoose Your Operation", fg='black', width=40, font=20)
second_label.pack(pady=40, padx=50)
Button(self, text="INSERT", width=10, bg='cyan', command=lambda: run.show_frame(InsertMenu), font=20).pack(pady=160, padx=20)
class InsertMenu(Frame):
def __init__(self, root, run):
global table_op
Frame.__init__(self, root)
Label(self, text="CREATE RECORD\n\nChoose Table to Begin", fg='black', width=20, font=("bold", 20)).pack(padx=130, pady=30)
table_op = IntVar()
Radiobutton(self, text="Personnel Records", variable=table_op, value=1).place(x=60, y=160)
Radiobutton(self, text="Office Inventory", variable=table_op, value=2).place(x=320, y=160)
Radiobutton(self, text="Company", variable=table_op, value=3).place(x=60, y=190)
Radiobutton(self, text="Office Accounting Records", variable=table_op, value=4).place(x=320, y=190)
Radiobutton(self, text="Contract Records", variable=table_op, value=5).place(x=60, y=220)
Button(self, text="Select", width=20, command=lambda: run.show_frame(InsertOperation), bg="green", fg="white").place(x=60, y=300)
class InsertOperation(Frame):
def __init__(self, parent, run):
Frame.__init__(self, parent)
Label(self, text="CREATE RECORD\n\nChoose Table to Begin", fg='black', width=20, font=("bold", 20)).pack(padx=130, pady=30)
table_select = table_op.get()
if table_select == 1:
Label(self, text="INSERT STAFF RECORDS", font=("bold", 20)).place(x=35, y=10)
Label(self, text="First Name", font=("bold", 12)).place(x=30, y=50)
Label(self, text="Last Name", font=("bold", 12)).place(x=30, y=100)
Label(self, text="Position", font=("bold", 12)).place(x=30, y=150)
Label(self, text="Address", font=("bold", 12)).place(x=30, y=200)
Label(self, text="Salary", font=("bold", 12)).place(x=30, y=250)
f_name = Entry(self, width=40)
f_name.place(x=120, y=55)
l_name = Entry(self, width=40)
l_name.place(x=120, y=105)
post = Entry(self, width=40)
post.place(x=120, y=155)
addy = Entry(self, width=40)
addy.place(x=120, y=205)
wages = Entry(self, width=40)
wages.place(x=120, y=255)
app = MyOMSApp()
app.mainloop()
没有错误信息
所有 类 都是在开始时创建的,因此 __init__
中的所有代码甚至在您看到 window 和 get()
之前就开始执行 select元素.
您必须在函数中使用 get()
,当您更改 pages/frames 时将执行该函数。
您可以在所有页面中添加功能即。 update_page
(不要使用名称 update()
因为 tkinter 已经使用了这个名称)并在 show frame
中执行为 my_ops.update_page()
现在您可以将 get()
移动到 update_page()
from tkinter import *
class MyOMSApp(Tk):
def __init__(self, *args, **kwargs):
Tk.__init__(self, *args, **kwargs)
container = Frame(self)
container.pack(side="top", fill="both")
container.grid_rowconfigure(0, weight=1)
container.grid_columnconfigure(0, weight=1)
self.frames = {}
for op in (MainPage, InsertMenu, InsertOperation):
my_ops = op(container, self)
self.frames[op] = my_ops
my_ops.grid(row=0, column=0, sticky='nsew')
my_ops.grid(row=550, column=550, sticky='nsew')
self.show_frame(MainPage)
def show_frame(self, cont):
my_ops = self.frames[cont]
my_ops.update_page() # <--
my_ops.tkraise()
class MainPage(Frame):
def __init__(self, root, run):
Frame.__init__(self, root)
main_label = Label(self, text="WELCOME TO THE OFFICE MANAGEMENT SYSTEM")
main_label.pack(pady=10, padx=10)
second_label = Label(self, text="Welcome\n\nChoose Your Operation", fg='black', width=40, font=20)
second_label.pack(pady=40, padx=50)
Button(self, text="INSERT", width=10, bg='cyan', command=lambda: run.show_frame(InsertMenu), font=20).pack(pady=160, padx=20)
def update_page(self):
pass
class InsertMenu(Frame):
def __init__(self, root, run):
global table_op
Frame.__init__(self, root)
Label(self, text="CREATE RECORD\n\nChoose Table to Begin", fg='black', width=20, font=("bold", 20)).pack(padx=130, pady=30)
table_op = IntVar()
Radiobutton(self, text="Personnel Records", variable=table_op, value=1).place(x=60, y=160)
Radiobutton(self, text="Office Inventory", variable=table_op, value=2).place(x=320, y=160)
Radiobutton(self, text="Company", variable=table_op, value=3).place(x=60, y=190)
Radiobutton(self, text="Office Accounting Records", variable=table_op, value=4).place(x=320, y=190)
Radiobutton(self, text="Contract Records", variable=table_op, value=5).place(x=60, y=220)
Button(self, text="Select", width=20, command=lambda: run.show_frame(InsertOperation), bg="green", fg="white").place(x=60, y=300)
def update_page(self): # <--
pass
class InsertOperation(Frame):
def __init__(self, parent, run):
Frame.__init__(self, parent)
Label(self, text="CREATE RECORD\n\nChoose Table to Begin", fg='black', width=20, font=("bold", 20)).pack(padx=130, pady=30)
def update_page(self): # <--
table_select = table_op.get()
print(table_select)
if table_select == 1:
Label(self, text="INSERT STAFF RECORDS", font=("bold", 20)).place(x=35, y=10)
Label(self, text="First Name", font=("bold", 12)).place(x=30, y=50)
Label(self, text="Last Name", font=("bold", 12)).place(x=30, y=100)
Label(self, text="Position", font=("bold", 12)).place(x=30, y=150)
Label(self, text="Address", font=("bold", 12)).place(x=30, y=200)
Label(self, text="Salary", font=("bold", 12)).place(x=30, y=250)
f_name = Entry(self, width=40)
f_name.place(x=120, y=55)
l_name = Entry(self, width=40)
l_name.place(x=120, y=105)
post = Entry(self, width=40)
post.place(x=120, y=155)
addy = Entry(self, width=40)
addy.place(x=120, y=205)
wages = Entry(self, width=40)
wages.place(x=120, y=255)
app = MyOMSApp()
app.mainloop()
我的 Tkinter 程序中的一个页面没有加载。我不确定为什么
我的 InsertOperation
class 中有一个 get()
方法,它用于从前一个 class 中检索所选单选按钮的值并执行适当的部分我的代码。然而,这个 get()
方法似乎并没有这样做。有替代方案吗?
class MyOMSApp(Tk):
def __init__(self, *args, **kwargs):
Tk.__init__(self, *args, **kwargs)
container = Frame(self)
container.pack(side="top", fill="both")
container.grid_rowconfigure(0, weight=1)
container.grid_columnconfigure(0, weight=1)
self.frames = {}
for op in (MainPage, InsertMenu, InsertOperation):
my_ops = op(container, self)
self.frames[op] = my_ops
my_ops.grid(row=0, column=0, sticky='nsew')
my_ops.grid(row=550, column=550, sticky='nsew')
self.show_frame(MainPage)
def show_frame(self, cont):
my_ops = self.frames[cont]
my_ops.tkraise()
class MainPage(Frame):
def __init__(self, root, run):
Frame.__init__(self, root)
main_label = Label(self, text="WELCOME TO THE OFFICE MANAGEMENT SYSTEM")
main_label.pack(pady=10, padx=10)
second_label = Label(self, text="Welcome\n\nChoose Your Operation", fg='black', width=40, font=20)
second_label.pack(pady=40, padx=50)
Button(self, text="INSERT", width=10, bg='cyan', command=lambda: run.show_frame(InsertMenu), font=20).pack(pady=160, padx=20)
class InsertMenu(Frame):
def __init__(self, root, run):
global table_op
Frame.__init__(self, root)
Label(self, text="CREATE RECORD\n\nChoose Table to Begin", fg='black', width=20, font=("bold", 20)).pack(padx=130, pady=30)
table_op = IntVar()
Radiobutton(self, text="Personnel Records", variable=table_op, value=1).place(x=60, y=160)
Radiobutton(self, text="Office Inventory", variable=table_op, value=2).place(x=320, y=160)
Radiobutton(self, text="Company", variable=table_op, value=3).place(x=60, y=190)
Radiobutton(self, text="Office Accounting Records", variable=table_op, value=4).place(x=320, y=190)
Radiobutton(self, text="Contract Records", variable=table_op, value=5).place(x=60, y=220)
Button(self, text="Select", width=20, command=lambda: run.show_frame(InsertOperation), bg="green", fg="white").place(x=60, y=300)
class InsertOperation(Frame):
def __init__(self, parent, run):
Frame.__init__(self, parent)
Label(self, text="CREATE RECORD\n\nChoose Table to Begin", fg='black', width=20, font=("bold", 20)).pack(padx=130, pady=30)
table_select = table_op.get()
if table_select == 1:
Label(self, text="INSERT STAFF RECORDS", font=("bold", 20)).place(x=35, y=10)
Label(self, text="First Name", font=("bold", 12)).place(x=30, y=50)
Label(self, text="Last Name", font=("bold", 12)).place(x=30, y=100)
Label(self, text="Position", font=("bold", 12)).place(x=30, y=150)
Label(self, text="Address", font=("bold", 12)).place(x=30, y=200)
Label(self, text="Salary", font=("bold", 12)).place(x=30, y=250)
f_name = Entry(self, width=40)
f_name.place(x=120, y=55)
l_name = Entry(self, width=40)
l_name.place(x=120, y=105)
post = Entry(self, width=40)
post.place(x=120, y=155)
addy = Entry(self, width=40)
addy.place(x=120, y=205)
wages = Entry(self, width=40)
wages.place(x=120, y=255)
app = MyOMSApp()
app.mainloop()
没有错误信息
所有 类 都是在开始时创建的,因此 __init__
中的所有代码甚至在您看到 window 和 get()
之前就开始执行 select元素.
您必须在函数中使用 get()
,当您更改 pages/frames 时将执行该函数。
您可以在所有页面中添加功能即。 update_page
(不要使用名称 update()
因为 tkinter 已经使用了这个名称)并在 show frame
中执行为 my_ops.update_page()
现在您可以将 get()
移动到 update_page()
from tkinter import *
class MyOMSApp(Tk):
def __init__(self, *args, **kwargs):
Tk.__init__(self, *args, **kwargs)
container = Frame(self)
container.pack(side="top", fill="both")
container.grid_rowconfigure(0, weight=1)
container.grid_columnconfigure(0, weight=1)
self.frames = {}
for op in (MainPage, InsertMenu, InsertOperation):
my_ops = op(container, self)
self.frames[op] = my_ops
my_ops.grid(row=0, column=0, sticky='nsew')
my_ops.grid(row=550, column=550, sticky='nsew')
self.show_frame(MainPage)
def show_frame(self, cont):
my_ops = self.frames[cont]
my_ops.update_page() # <--
my_ops.tkraise()
class MainPage(Frame):
def __init__(self, root, run):
Frame.__init__(self, root)
main_label = Label(self, text="WELCOME TO THE OFFICE MANAGEMENT SYSTEM")
main_label.pack(pady=10, padx=10)
second_label = Label(self, text="Welcome\n\nChoose Your Operation", fg='black', width=40, font=20)
second_label.pack(pady=40, padx=50)
Button(self, text="INSERT", width=10, bg='cyan', command=lambda: run.show_frame(InsertMenu), font=20).pack(pady=160, padx=20)
def update_page(self):
pass
class InsertMenu(Frame):
def __init__(self, root, run):
global table_op
Frame.__init__(self, root)
Label(self, text="CREATE RECORD\n\nChoose Table to Begin", fg='black', width=20, font=("bold", 20)).pack(padx=130, pady=30)
table_op = IntVar()
Radiobutton(self, text="Personnel Records", variable=table_op, value=1).place(x=60, y=160)
Radiobutton(self, text="Office Inventory", variable=table_op, value=2).place(x=320, y=160)
Radiobutton(self, text="Company", variable=table_op, value=3).place(x=60, y=190)
Radiobutton(self, text="Office Accounting Records", variable=table_op, value=4).place(x=320, y=190)
Radiobutton(self, text="Contract Records", variable=table_op, value=5).place(x=60, y=220)
Button(self, text="Select", width=20, command=lambda: run.show_frame(InsertOperation), bg="green", fg="white").place(x=60, y=300)
def update_page(self): # <--
pass
class InsertOperation(Frame):
def __init__(self, parent, run):
Frame.__init__(self, parent)
Label(self, text="CREATE RECORD\n\nChoose Table to Begin", fg='black', width=20, font=("bold", 20)).pack(padx=130, pady=30)
def update_page(self): # <--
table_select = table_op.get()
print(table_select)
if table_select == 1:
Label(self, text="INSERT STAFF RECORDS", font=("bold", 20)).place(x=35, y=10)
Label(self, text="First Name", font=("bold", 12)).place(x=30, y=50)
Label(self, text="Last Name", font=("bold", 12)).place(x=30, y=100)
Label(self, text="Position", font=("bold", 12)).place(x=30, y=150)
Label(self, text="Address", font=("bold", 12)).place(x=30, y=200)
Label(self, text="Salary", font=("bold", 12)).place(x=30, y=250)
f_name = Entry(self, width=40)
f_name.place(x=120, y=55)
l_name = Entry(self, width=40)
l_name.place(x=120, y=105)
post = Entry(self, width=40)
post.place(x=120, y=155)
addy = Entry(self, width=40)
addy.place(x=120, y=205)
wages = Entry(self, width=40)
wages.place(x=120, y=255)
app = MyOMSApp()
app.mainloop()