'Getting a variable out of an Entry text zone
I'm coding an Ip Calculator to train with Python and Tkinter. As far as I am, I managed to create a UI with a text zone in which the user will write their IP. When they press the OK button, the IP is assigned to the ip_addr var, I tried to print it and it seems to work.
But when I try to use it to display netmask or network it seems my var is nowhere to be found. How can I retrieve this variable to display result in a second text zone, using the second piece of code (don't mind the print() in it, I will properly embed it in Label and Frame later)
import ipaddress
### For Tkinter import problem
try:
from Tkinter import *
except ImportError:
from tkinter import *
def ok_env():
ip_addr = zone_text.get()
#zone_text.delete(0,END)
print(ip_addr) #La variable est bien recuperée quand on clique sur le bouton ok, mais pas transferée?
ipcalc_window = Tk()
ipcalc_window.title("IP Calculator")
ipcalc_window.geometry('768x512')
ipcalc_window.minsize(768, 512)
ipcalc_window.config(background='#C5D0DB')
ipcalc_mainframe = Frame(ipcalc_window, bg='#C5D0DB')
ipcalc_subframe = Frame(ipcalc_window, bg='white')
label_title = Label(ipcalc_mainframe, text="Entrez l'adresse IP, au format XXX.XXX.XXX.XXX/XX:", bg='#C5D0DB', font=('Consolas', 16))
label_title.pack(pady=5)
zone_text = Entry(ipcalc_mainframe, width=45, bg='white', font=('Consolas', 16))
zone_text.pack()
ok_button = Button(ipcalc_mainframe, text="OK", font=('Consolas', 16), command=ok_env)
ok_button.pack(pady=5)
label_text = Label(ipcalc_subframe, font=('Consolas', 16) )
label_text.pack()
ipcalc_mainframe.pack()
ipcalc_subframe.pack()
ipcalc_window.mainloop()
### Piece of code not working
info4 = ipaddress.ip_interface(ip_addr)
print("> [+] IP:", ip_addr, "\n> [+]")
print("> [+] Addresse Reseau:", info4.network)
Hope my English is not to bad, thanks to those who will take the time to read it.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
