【问题标题】:Tkinter page not loadingTkinter 页面未加载
【发布时间】:2019-07-22 20:35:03
【问题描述】:

我的 Tkinter 程序中的一个页面没有加载。我不知道为什么

我的InsertOperation 类中有一个get() 方法,用于从上一个类中检索选定单选按钮的值并执行我的代码的适当部分。但是,这个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()

没有错误信息

【问题讨论】:

  • 您是否收到错误消息?哪个get() 不起作用?所有页面都是在开始时创建的(甚至在您看到窗口之前),因此如果您在__init__ 中使用get(),那么它会在您使用任何小部件之前执行。您可能必须在更改页面时执行的函数中使用get()
  • 如果您在所有__init__ 中使用print(),那么您会看到它们在您看到窗口之前被执行 - 当小部件为空且尚未选择时。
  • 这个get() 方法在定义的类中的什么位置?请edit您的问题并添加它。
  • @martineau 在 InserOperation 类的第 4 行
  • 这行代码不起作用:###table_select = table_op.get().###。 InserOperation 类中的第 4 行

标签: python tkinter init


【解决方案1】:

所有类都是在开始时创建的,所以__init__ 中的所有代码甚至在你看到窗口之前就在开始时执行,get() 在你可以选择元素之前取值。

您必须在更改页面/框架时执行的函数中使用get()

您可以在所有页面中添加功能,即。 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()

【讨论】:

  • 谢谢@furas。不得不移动其他一些代码行。但效果很好。
猜你喜欢
  • 2017-10-11
  • 2013-09-15
  • 1970-01-01
  • 2016-06-10
  • 2020-04-04
  • 2017-08-18
  • 2020-08-03
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多