Patrick260284
Patrick260284

Reputation: 89

Python changing Label Text depending on Optionmenu selection via button

Ok here is the situation. I have a optionmenu with two values: eth0 & wlan0 (german for wifi) I have a button that triggers a function. In that function I check which option is selected and then it should change a Labels text. But when I click the button nothing happens. What am I missing?

Here´s the code:

Programm start:

from Tkinter import *
import subprocess


root = Tk()
#root.attributes('-fullscreen',True)
root.geometry("480x320")
root.title("BabyDroid")

subprocess.call(["ip addr show eth0 | grep -Eo '[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}' > ip.txt"],shell=True)
ip_file= open("ip.txt")
textip = StringVar()
textip.set(ip_file.readline())
ip_file.close()

Dropdown menu:

network = StringVar(root)                                                           #Networkstatevariable for dropdown
network.set("eth0")                                                                 #default
networkmenu = OptionMenu(root,network,"eth0","wlan0").place(relx=0.8,rely=0.1)      #actual Dropdownmenu

Function:

def get_ip_address():
    global network
    global ip_file
    global textip
    if network.get() == "eth0":
        subprocess.call(["ip addr show eth0 | grep -Eo '[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}' > ip.txt"],shell=True)
        ip_file = open("/home/pi/RPi_Cam_Web_Interface/ip.txt")
        textip.set(ip_file.readline())
        ip_file.close()

    else:
        subprocess.call(["ip addr show wlan0 | grep -Eo '[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}' > ip.txt"],shell=True)
        ip_file= open("/home/pi/RPi_Cam_Web_Interface/ip.txt")
        textip.set(ip_file.readline())
        ip_file.close()

Label:

IPLabel = Label(root,textvariable=textip).place(relx=0.5,rely=0.1)

Button:

button4 = Button(root,text="Aktualisieren",fg="black",height=3,width=8,command=get_ip_address).place(relx=0.8,rely=0.2)

Upvotes: 0

Views: 587

Answers (1)

Ben J.
Ben J.

Reputation: 76

In Tkinter, in order to retrieve a value from a dropdown menu, or any other widget that takes user input, you must use the get() function. Such as:

if network.get() == "eth0":

That is the only problem with your code that I have found so far.

Upvotes: 2

Related Questions