Python通过按钮

时间:2016-03-12 17:22:14

标签: python tkinter

好的就是这种情况。 我有一个有两个值的选项菜单:eth0& wlan0(德语为wifi) 我有一个触发功能的按钮。在该函数中,我检查选择了哪个选项,然后它应该更改标签文本。但是当我点击按钮时没有任何反应。我错过了什么?

这是代码:

程序启动:

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()

下拉菜单:

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

功能:

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()

标签:

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

按钮:

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

1 个答案:

答案 0 :(得分:2)

在Tkinter中,为了从下拉菜单或任何其他需要用户输入的窗口小部件中检索值,必须使用get()函数。如:

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

这是我到目前为止找到的代码唯一的问题。