【问题标题】:tkinter access widget on a different windowtkinter 访问不同窗口上的小部件
【发布时间】:2015-04-16 09:55:21
【问题描述】:

因此,我对 tkiner 编程还很陌生,并且尝试使用具有多个窗口的 GUI 来扫描 Wifi-Hotspots 并向我显示它们的列表。我从Switch between two frames in tkinter 复制了一个示例来获取不同的窗口

我有一个主菜单来启用 Wifi 卡的监控模式并开始扫描。我正在调用另一个带有列表框的框架来显示结果。

我现在的问题是,函数 startScan(self) 在 StartPage 中被调用,而 Listbox 在 PageOne 中。我怎样才能在那里寻址并添加条目?

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

        # the container is where we'll stack a bunch of frames
        # on top of each other, then the one we want visible
        # will be raised above the others
        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 (StartPage, PageOne, PageTwo):
            frame = F(container, self)
            self.frames[F] = frame
            # put all of the pages in the same location; 
            # the one on the top of the stacking order
            # will be the one that is visible.
            frame.grid(row=0, column=0, sticky="nsew")

        self.show_frame(StartPage)

    def show_frame(self, c):
         '''Show a frame for the given class'''
        frame = self.frames[c]
        frame.tkraise()

class StartPage(tk.Frame):
    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent) 
        label = tk.Label(self, text="Main Menu", font=TITLE_FONT)
        label.pack(side="top", fill="x", pady=10)

        self.var = tk.IntVar()
        monCheck = tk.Checkbutton(self, text="Monitor Mode", variable=self.var, command=self.monSwitch)

        scanButton = tk.Button(self, text="Start Scan", command=self.startScan)
        quitButton = tk.Button(self, text="Quit", command=self.master.quit)
        monCheck.pack()
        scanButton.pack()
        quitButton.pack()

    def monSwitch(self):
        if(self.var.get()):
             print "Monitor Modus an"
             check_call(["airmon-ng", "start", "wlan0"])
        else:
             print "Monitor Modus aus"
             check_call(["airmon-ng", "stop", "mon0"])

    def startScan(self):
        print "Scan gestartet"
        app.show_frame(PageOne)
        output=check_output('iwlist wlan0 scan | grep -E "Channel:|ESSID:"', shell=True)
        netze = output.split()
        print netze
        for i in range(0,(len(netze)/2)-1): 
              string = netze[2*i]+" "+netze[2*i+1]    
              app.frames[PageOne].netzList.insert(END, string)     


class PageOne(tk.Frame):
    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)     
        label = tk.Label(self, text="Scanergebnisse", font=TITLE_FONT)
        label.pack(side="top", fill="x", pady=10)
        menuButton = tk.Button(self, text="Menu", command=lambda: controller.show_frame(StartPage))
         quitButton = tk.Button(self, text="Quit", command=self.master.quit)
         button = tk.Button(self, text="P2", command=lambda: controller.show_frame(PageTwo))
         netzList = tk.Listbox(self, width=30)

        netzList.pack()
        quitButton.pack(side=LEFT)
        menuButton.pack(side=LEFT)
        button.pack(side=LEFT)


class PageTwo(tk.Frame):
    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)
        label = tk.Label(self, text="This is page 2", font=TITLE_FONT)
        label.pack(side="top", fill="x", pady=10)
        button = tk.Button(self, text="Go to the start page", 
                       command=lambda: controller.show_frame(StartPage))
        button.pack()    

if __name__ == "__main__":
app = PyWiFi()
app.resizable(0, 0)
app.geometry("320x240")
app.mainloop()

【问题讨论】:

  • app 未在 StartPage 中定义。相反,您应该在那里使用self.controller 并将self.controller = controller 放在StartPage__init__ 中。但是,由于您的代码包含许多未包含在代码中的功能和其他内容,因此我无法运行它并查看该 fixel 是否所有内容。下次请发MCVE
  • 好吧,我访问应用程序没有问题。我得到的错误是 AttributeError: PageOne instance has no attribute 'netzList'。所以看起来代码可以访问PageOne,但找不到netzList。我不知道为什么。
  • 我已经添加了完整的代码。
  • 好的,在您的问题中提及该错误消息会很有用。现在我看到了,netzlistPageOne 中定义的所有其他小部件都是__init__ 的本地部件。您应该将它们命名为 self.netzlist 等,以便以后能够引用它们。
  • 是的,抱歉.. 好的,已经解决了。以为我已经尝试过了。谢谢!如果你愿意,你可以把你的答案写下来,这样我就可以把它标记为已回答。

标签: python class tkinter


【解决方案1】:

netzlistPageOne 中定义的所有其他小部件都是__init__ 的本地小部件。您应该将它们命名为self.netzlist 等,以便稍后能够将它们作为PageOne 的类属性引用。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2010-09-26
    • 1970-01-01
    • 2017-11-06
    • 2020-01-29
    • 1970-01-01
    • 2018-07-01
    • 1970-01-01
    • 2019-02-03
    相关资源
    最近更新 更多