无法在选项之间切换

2024-10-01 00:14:24 发布

您现在位置:Python中文网/ 问答频道 /正文

我想做一个简单的应用程序,可以显示我的网络接口卡的基本信息。应用程序正在工作,但当我切换单选按钮时,它总是显示相同的输出。我怎样才能解决这个问题? 我不知道它将如何在一个没有安装wifi或以太网驱动程序的设备上工作,但这不是我要问的问题。nicInfo返回一个元组,根据单选按钮,它将返回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()

Tags: textself信息indexdefcolumnrootgrid
1条回答
网友
1楼 · 发布于 2024-10-01 00:14:24

你的问题是,你的单选按钮没有任何功能,如果你点击它们。所以你的show()方法只会被调用一次,这就是为什么你的文本不会改变。使用单选按钮的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

相关问题 更多 >