Reputation: 65
I am trying to make a simple application which could show me basic information about my network interface card. The application is working but when I am switching the radio buttons it always shows the same output. How can I fix this? I am not sure how it would work on a device which does not have a wifi or ethernet driver installed but that's not the thing which I am asking. The nicInfo returns a tupple and based on the radio buttons it will return MAC and IP address of ehternet or Wi-FI and when it is set to have the initial value zero it will display the info of ethernet and when I try to switch it to wifi it still shows the info of the ethernet.
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()
Upvotes: 0
Views: 66
Reputation: 1498
Your problem is that your radiobuttons doesn't have any function if you click them. So your show()
method will be only called once, thats why your text doesn't change. Use the command
option of your Radiobuttons to bind a method to them. This results in this code:
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()
Nevertheless i was getting a Exception: KeyError: 'broadcast'
from nicInfo()
after i switched to WiFi
, even thouh i have wifi
Upvotes: 1