0

I'm making a GUI with python and tkinter That prompts user the Mac Address of his pc and asks for a code
The Python snippet i have used for retrieving the MAc address is :

import uuid

def get_mac():
    mac_num = hex(uuid.getnode()).replace('0x', '').upper()
    mac = ''.join(mac_num[i : i + 2] for i in range(0, 11, 2))
    return mac

x= get_mac()
print x

I have also made a gui containing the two fields as shown below

enter image description here

However when i execute the python snippet the mac address is displayed outside the python gui and in the python shell, how can i make the mac address appear in the space provided in the GUi itself

Here is the code for the gui:

from Tkinter import *
from ttk import *
root =Tk()
def show_form():
bottomFrame = Frame(root)
bottomFrame.pack(side=BOTTOM)

b = Button(bottomFrame,text="ACTIVATE",command=lambda: show_call_back(root))
b1 = Button(bottomFrame, text="TRIM")
b2 = Button(bottomFrame, text="OVERLAY")
b3 = Button(bottomFrame, text="MERGE")

b.pack(side=RIGHT,padx=8,pady=26)
b1.pack(side=LEFT, padx=8, pady=26)
b1.config(state='disabled')
b2.pack(side=LEFT, padx=8, pady=26)
b2.config(state='disabled')
b3.pack(side=LEFT, padx=8, pady=26)
b3.config(state='disabled')

root.mainloop()



def show_call_back(parent):

top = Toplevel(parent)
top.geometry("250x200+600+250")
top.resizable(width=False, height=False)

top.title("Activation")
Label(top, text="Mac Address:",).grid(row=0, sticky=W, padx=4)


Label(top, text="Code").grid(row=1, sticky=W, padx=4)
Entry(top).grid(row=1, column=1, sticky=E, pady=4)
Button(top, text="Submit", command=top.destroy).grid(row=2, column=1)


show_form()
root.mainloop()
  • You have added the tag `tkinter` but in the provided source code, that graphic library is not used. Please, provide the source code of the snippet. Note: the Mac address is display in the shell because you are using the standard `print` function. – J. Piquard Mar 05 '17 at 15:05
  • Added the code now,please check –  Mar 05 '17 at 15:12
  • 1
    First off preferably you should only have one mainloop – Taku Mar 05 '17 at 15:19
  • Why you are using an `Entry()` to display a value which is read with the function `get_mac()` ? – J. Piquard Mar 05 '17 at 15:44
  • entry is for the code,since the user will have to input it(that code will be different), i just need the mac address to be displayed beside the mac section –  Mar 05 '17 at 15:47

1 Answers1

0

After your last comment, the solution is quite simple: add a new Label to display the result of get_mac().

Solution - Add a Label in row=0 and text=get_mac().

hLbl = Label(top, text=get_mac(), bg='white', relief=SUNKEN, width = 15)
hLbl.grid(row=0, column=1, sticky=E, pady=4)

I have added bg='white' and relief=SUNKEN to a the same style as an Entry. The extra width = 15 is to enlarge the size of the label.

Warning 1 - as @abccd comments, keep only one mainloop(), and place the root = Tk() after the function declaration.

Warning 2 - Instead of using root as a global variable in bottomFrame = Frame(root) of the function show_form(), add it as input parameter.

def show_form(my_root): # use my_root instead of global root
    bottomFrame = Frame(my_root)
    bottomFrame.pack(side=BOTTOM)
    #  also for the command parameter
    b = Button(bottomFrame,text="ACTIVATE",command=lambda: show_call_back(my_root))
    ...

And call:

root = Tk()
show_form(root)
root.mainloop()

EDIT -------

Output - here is what I get under Python 3.5.0

enter image description here

J. Piquard
  • 1,665
  • 2
  • 12
  • 17
  • Phew Thanks man...is there anyway to include "Mac Address:" before the actual address gets displayed, kind of a newbie in tkinter –  Mar 05 '17 at 16:57
  • @JustinJoy Did you get what I have added in my answer ? – J. Piquard Mar 05 '17 at 17:03
  • Ah yes,I actually deleted a line that prints the label of "Mac address:", all is good now,the output is same as shown above , thank you so much.. could i ask from what sources did you learn tkinter? –  Mar 05 '17 at 17:11
  • Humm, I am self learning. I use the SO Question to test and explore Python. The tkinter library is funny and I play with to find SO Answer as for you. Thank asking help with your problem !!! – J. Piquard Mar 05 '17 at 17:15
  • I need help regarding the same problem , http://stackoverflow.com/questions/42710221/error-in-tkinter-gui , Please check it out @J.piquard –  Mar 10 '17 at 05:21