【问题标题】:Accessing a tkinter list-box selection when using frames使用框架时访问 tkinter 列表框选择
【发布时间】:2018-12-20 16:09:33
【问题描述】:

当我用户选择一系列选项时,我试图在 tkinter 窗口中使用框架来更改布局 - 在本例中为“打开”。

我想要更新框架,但我还需要捕获列表框的选择。我试图从“openMat”方法中访问选择。

我已经尽可能地简化了代码。 我已经尝试解决这个问题一段时间了,尝试在网上寻找解决方案,最后还是点击了“提问”按钮。

import tkinter as tk
LARGE_FONT = ("Verdana", 12) # font's family is Verdana, font's size is 12 

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

        # text for all windows
        label2 = tk.Label(self, text='title', font=LARGE_FONT)
        label2.pack(pady=10, padx=10) # center alignment

        # this container contains all the pages
        container = tk.Frame(self)
        container.pack(side="top", fill="both", expand=True)
        container.grid_rowconfigure(0, weight=1)   # make the cell in grid cover the entire window
        container.grid_columnconfigure(0,weight=1) # make the cell in grid cover the entire window

        self.frames = {} # these are pages we want to navigate to

        for F in (StartPage, Page2): # for each page
            frame = F(container, self) # create the page
            self.frames[F] = frame  # store into frames
            frame.grid(row=0, column=0, sticky="nsew") # grid it to container

        self.show_frame(StartPage) # let the first page is StartPage


    def show_frame(self, name):
        frame = self.frames[name]
        frame.tkraise()

class StartPage(tk.Frame):
    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)

        ltbox = tk.Listbox(self)
        label = tk.Label(self, text='Menu', font=LARGE_FONT)

        label.grid(row=0, column = 0)
        #label.pack(pady=10, padx=10) # center alignment

        button1 = tk.Button(self, text='Open', width = 12,  # when click on this button, call the show_frame method to make PageOne appear
                            command=self.openMat)
        button1.grid(row=1, column = 0)
        #button1.pack() # pack it in


        #Insert data in listbox
        ltbox.insert( 1, "Option 1")
        ltbox.insert( 2, "Option 2")
        ltbox.insert( 3, "Option 3")
        ltbox.insert( 4, "Option 4")

        ltbox.grid(row=1, column = 4, rowspan=100, pady=0, padx=50)

        print (ltbox.curselection())

    def openMat(self):
        #This function prints the option selected and changes the frame
        print (ltbox.curselection())
        app.show_frame(Page2)



class Page2(tk.Frame):
    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)

        label = tk.Label(self, text='Page Two', font=LARGE_FONT)
        label.pack(pady=10, padx=10)

        button1 = tk.Button(self, text='Back to Home', # likewise StartPage
                            command=lambda : controller.show_frame(StartPage))
        button1.pack()


if __name__ == '__main__':
    app = MainWindow()
    app.mainloop()

这给出了错误:

NameError: name 'ltbox' is not defined

感谢您阅读我的问题 - 非常感谢任何帮助!

【问题讨论】:

    标签: python tkinter


    【解决方案1】:

    您的问题属于范围

    ltbox 已定义,因此只能在 StartPage 类的 __init__ 函数内使用。如果你想让一个类的所有函数都可以访问它,你必须让它成为这个类的实例属性,这是通过使用self来完成的。因此,无论您在哪里使用过 ltbox,只需将其更改为 self.ltbox

    【讨论】:

      猜你喜欢
      • 2012-07-18
      • 2020-06-23
      • 1970-01-01
      • 1970-01-01
      • 2021-07-18
      • 1970-01-01
      • 2011-09-27
      • 1970-01-01
      相关资源
      最近更新 更多