在另一个窗口上的tkinter访问小部件

时间:2015-04-16 09:55:22

标签: python class tkinter

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

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

我现在的问题是,在StartPage中调用函数startScan(self),而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()

1 个答案:

答案 0 :(得分:2)

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