无法在选项

时间:2015-08-09 14:23:28

标签: python tkinter

我正在尝试制作一个简单的应用程序,它可以显示有关我的网络接口卡的基本信息。该应用程序正在运行,但当我切换单选按钮时,它总是显示相同的输出。我怎样才能解决这个问题? 我不确定它如何在没有安装wifi或以太网驱动程序的设备上运行,但这不是我要问的问题。 nicInfo返回一个tupple,根据单选按钮,它将返回ehternet或Wi-FI的MAC和IP地址,当它被设置为初始值为零时,它将显示以太网的信息,当我尝试将其切换为wifi它仍然显示以太网的信息。

from Tkinter import *
from netifaces import ifaddresses, interfaces
from re import match

class App(Frame):

    def __init__(self, root):
        Frame.__init__(self, root)
        self.option_add('*Font', 'Verdana 10 bold')
        self.grid()
        self.create_widgets()
        self.show()

    def create_widgets(self):
        self.whichOne = IntVar()
        self.output = StringVar()
        self.whichOne.set(0)
        Radiobutton(self, text = 'Ethernet', padx = 10, pady = 10, variable = self.whichOne, value =0).grid(row=0, column=0, sticky=W)
        Radiobutton(self, text = 'Wi-Fi', padx = 10, pady = 10, variable = self.whichOne, value =1).grid(row=1, column=0, sticky=W)
        Button(self, text='QUIT', fg='red', command=self.quit).grid(row=0, column=1, sticky=W, rowspan=2, columnspan=2)

    def show(self):
        self.hexInt = interfaces()
        if True:
            self.nic = self.nicInfo(self.whichOne.get())
            selection = 'The MAC address of your device is %s\n' % (self.nic[0]) + '\nThe IP address of your device is %s\n' % (self.nic[1])
            Label(text=selection).grid(row=2, column=0, columnspan=2)

    def nicInfo(self, index):
        self.mac = 'unknown'
        for mainKey in ifaddresses(self.hexInt[index]): # a DICT which contains a LIST, in which is a DICT
            for subKey in ifaddresses(self.hexInt[index])[mainKey][0]: #this zero has to be here
                if match(r'([0-9a-f]{2}[:-]){5}([0-9a-f]{2})', ifaddresses(self.hexInt[index])[mainKey][0][subKey]):
                    self.mac = ifaddresses(self.hexInt[index])[mainKey][0][subKey].upper()
                elif match(r'((2[0-5]|1[0-9]|[0-9])?[0-9]\.){3}((2[0-5]|1[0-9]|[0-9])?[0-9])', ifaddresses(self.hexInt[0])[mainKey][0][subKey]) and subKey == 'addr':
                    self.ip = ifaddresses(self.hexInt[index])[mainKey][0][subKey]
        return self.mac, self.ip

if __name__ == '__main__':
    root = Tk()
    root.title('MAC')
    app = App(root)
    app.mainloop()

1 个答案:

答案 0 :(得分:1)

您的问题是,如果单击它们,您的单选按钮不具备任何功能。因此,您的show()方法只会被调用一次,这就是为什么您的文字没有变化的原因。使用Radiobuttons的command选项将方法绑定到它们。这导致了这段代码:

class App(Frame):

    def __init__(self, root):
        Frame.__init__(self, root)
        self.option_add('*Font', 'Verdana 10 bold')
        self.grid()
        self.create_widgets()
        self.show()

    def create_widgets(self):
        self.whichOne = IntVar()
        self.output = StringVar()
        self.whichOne.set(0)
        Radiobutton(self, text = 'Ethernet', padx = 10, pady = 10, variable = self.whichOne, value =0, command=self.show).grid(row=0, column=0, sticky=W)
        Radiobutton(self, text = 'Wi-Fi', padx = 10, pady = 10, variable = self.whichOne, value =1, command=self.show).grid(row=1, column=0, sticky=W)
        Button(self, text='QUIT', fg='red', command=self.quit).grid(row=0, column=1, sticky=W, rowspan=2, columnspan=2)

    def show(self):
        self.hexInt = interfaces()
        if True:
            self.nic = self.nicInfo(self.whichOne.get())
            selection = 'The MAC address of your device is %s\n' % (self.nic[0]) + '\nThe IP address of your device is %s\n' % (self.nic[1])
            Label(text=selection).grid(row=2, column=0, columnspan=2)

    def nicInfo(self, index):
        self.mac = 'unknown'
        for mainKey in ifaddresses(self.hexInt[index]): # a DICT which contains a LIST, in which is a DICT
            for subKey in ifaddresses(self.hexInt[index])[mainKey][0]: #this zero has to be here
                if match(r'([0-9a-f]{2}[:-]){5}([0-9a-f]{2})', ifaddresses(self.hexInt[index])[mainKey][0][subKey]):
                    self.mac = ifaddresses(self.hexInt[index])[mainKey][0][subKey].upper()
                elif match(r'((2[0-5]|1[0-9]|[0-9])?[0-9]\.){3}((2[0-5]|1[0-9]|[0-9])?[0-9])',
                           ifaddresses(self.hexInt[0])[mainKey][0][subKey]) and\
                           subKey == 'addr':
                    self.ip = ifaddresses(self.hexInt[index])[mainKey][0][subKey]
        return self.mac, self.ip

if __name__ == '__main__':
    root = Tk()
    root.title('MAC')
    app = App(root)
    app.mainloop()

然而,在我切换到KeyError: 'broadcast'之后,我从nicInfo()获得了例外WiFi,即使我有wifi