【问题标题】:Tkinter: refresh frame/window from an outside functionTkinter:从外部功能刷新框架/窗口
【发布时间】:2018-06-17 23:24:18
【问题描述】:

我想在updateCustomerList 完成更新列表时刷新MainPage,以便更新后的列表显示在MainPage 小部件上。

我尝试过使用tk.show_frame(<frame>) 等,但由于函数本身没有绑定到主 Tkinter 框架本身或者甚至不是 Tkinter 对象,所以我不完全确定如何重新加载页面.有什么建议吗?

下面的代码是我整个程序的sn-p:

customerList = [] #list is updated at the updateCustomerList function; global variable

class POS(tk.Tk):
    def __init__(self,*args,**kwargs):
        tk.Tk.__init__(self, *args, **kwargs)

        container = tk.Frame(self)
        container.pack(side = "top", fill = "both", expand = True)
        container.grid_rowconfigure(0, weight = 1)
        container.grid_columnconfigure(0, weight = 1)

        self.frames = {}
        for F in (ErrorPage, PaymentPage, MainPage): 
            frame = F(container, self)
            self.frames[F] = frame
            frame.grid(row=0, column = 0, sticky = "nsew")

        #show frame here


class MainPage(tk.Frame):
    def __init__(self, parent, controller):
        tk.Frame.__init__(self,parent)
        """
        Tkinter frame that I would like to "refresh" and use the new updated list
        """
        frame5Button = ttk.Button(frame5, text = "Add Item", command = lambda: updateCustomerList(barCode, quantity))
        frame5Button.grid(row = 0, column = 5, padx = 90, pady = 10)
        #This button allows me to go into the updateCustomerList function

def updateCustomerList(barCode, quantity):
    #some code to update a list
    #when function finishes updating the list, I would like to go back to the MainPage Tk frame and reload all the widgets like labels and entry boxes using the updated customerList list

app = POS()
app.geometry("700x700") 
app.resizable(False, False)
app.after(100, MasterFilePopUp)
app.mainloop()

【问题讨论】:

  • 代码中的缩进不正确(参见for F ... 循环)
  • 已修复!谢谢你告诉我

标签: python tkinter reload


【解决方案1】:

只需删除并重新创建 Mainframe 的实例,作为 updateCustomerList 的最后一行:

container = 0 #global variable

在 POS(tk.Tk) 中添加“全局容器”

def updateCustomerList(barCode, quantity):
    global app
    ...
    app.frames[MainPage].destroy()
    app.frames[MainPage] = MainPage(container, app)
    app.frames[MainPage].grid(row=0, column = 0, sticky = "nsew")
    app.frames[MainPage].tkraise() 
#function ends here

【讨论】:

  • 它不是在全局范围内创建的。它是在 POS() 类中本地创建的,或者至少这是我的理解。尽管如此,我会试一试!
  • 那么POS到底是怎么创建的呢?
  • 更新了上面发布的代码以显示我是如何创建框架的
  • @Miggy 更新了我的答案。
  • 我认为它应该可以工作,但我仍然需要解决一些问题。当我开始的时候会回来的。谢谢!
猜你喜欢
  • 1970-01-01
  • 2014-01-27
  • 2010-12-29
  • 1970-01-01
  • 2020-04-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-03-09
相关资源
最近更新 更多