0

有没有办法让 Tkinter 中的条目在您在字段中键入时做出反应?我想要代码的“数量”部分使用此功能。

它会在单击单选按钮时自动生成 label4text,但在输入数量时不会。

from Tkinter import *

SubnetM = 0

def beenclicked():
    radioValue = relStatus.get()
    return

def changeLabel():
    if relStatus.get() == 'HOSTS':
        if custname.get() == 6:
            label4Text.set("255.255.255.248")
            return
    if relStatus.get() == 'NETWORKS':
        if custname.get() == 6:
            label4Text.set("224.0.0.0")
            return

app = Tk()
app.title("SUBNET MASK CALCULATOR")
app.geometry('400x450+200+200')

labelText = StringVar()
labelText.set("WELCOME!")
label1 = Label(app,textvariable=labelText, height=4)
label1.pack()

relStatus = StringVar()
relStatus.set(None)
radioValue = relStatus.get()
radio1 = Radiobutton(app, text="HOSTS", value="HOSTS", variable=relStatus, command=changeLabel)
radio1.pack()
radio1 = Radiobutton(app, text="NETWORKS", value="NETWORKS", variable=relStatus, command=changeLabel)
radio1.pack()

label2Text = StringVar()
label2Text.set("~Quantity~")
label2 = Label(app, textvariable=label2Text, height=4)
label2.pack()

custname = IntVar(None)
Quantity = Entry(app, textvariable=custname,)
Quantity.pack()

label3Text = StringVar()
label3Text.set("Your Subnet Mask is...")
label3 = Label(app, textvariable=label3Text, height=4)
label3.pack()

label4Text = StringVar()
label4Text.set(SubnetM)
label4 = Label(app, textvariable=label4Text, height=4)
label4.pack()

button1 = Button(app, text="GO!", width=20, command=changeLabel)
button1.pack(padx=15, pady=15)

app.mainloop()
4

1 回答 1

2

您可以使用trace()for在更改时StringVar()调用。changeLabelcustname

custname.trace("w", changeLabel)

custnameIntVarto更改为StringVar因为Entry在值为空时将值转换为 int 时出现问题(无数字)

我添加*argschangeLabel()是因为trace向函数发送一些参数。

完整代码:

from Tkinter import *

SubnetM = 0

def beenclicked():
    radioValue = relStatus.get()
    return

def changeLabel(*args): # require *args for trace
    print "changeLabel", args
    if relStatus.get() == 'HOSTS':
        if custname.get() == "6":
            label4Text.set("255.255.255.248")
            return
    if relStatus.get() == 'NETWORKS':
        if custname.get() == "6":
            label4Text.set("224.0.0.0")
            return

app = Tk()
app.title("SUBNET MASK CALCULATOR")
app.geometry('400x450+200+200')

labelText = StringVar()
labelText.set("WELCOME!")
label1 = Label(app,textvariable=labelText, height=4)
label1.pack()

relStatus = StringVar()
relStatus.set(None)
radioValue = relStatus.get()
radio1 = Radiobutton(app, text="HOSTS", value="HOSTS", variable=relStatus, command=changeLabel)
radio1.pack()
radio1 = Radiobutton(app, text="NETWORKS", value="NETWORKS", variable=relStatus, command=changeLabel)
radio1.pack()

label2Text = StringVar()
label2Text.set("~Quantity~")
label2 = Label(app, textvariable=label2Text, height=4)
label2.pack()

custname = StringVar()
custname.set("0")
Quantity = Entry(app, textvariable=custname,)
Quantity.pack()
custname.trace("w", changeLabel)


label3Text = StringVar()
label3Text.set("Your Subnet Mask is...")
label3 = Label(app, textvariable=label3Text, height=4)
label3.pack()

label4Text = StringVar()
label4Text.set(SubnetM)
label4 = Label(app, textvariable=label4Text, height=4)
label4.pack()

button1 = Button(app, text="GO!", width=20, command=changeLabel)
button1.pack(padx=15, pady=15)

app.mainloop()

仅在 Python 2.7 上测试

编辑:

我又添加了一些修改:

def changeLabel(*args): # require *args for trace
    print "changeLabel", args

    custname_int = 0 # default value when error
    try:
        custname_int = int( custname.get() )
    except:
        pass # if error do nothing

    if 2 < custname_int <= 6:
        if relStatus.get() == 'HOSTS':
            label4Text.set("255.255.255.248")
        else: #if relStatus.get() == 'NETWORKS':
            label4Text.set("224.0.0.0")
    else:
        label4Text.set("-")

编辑:

这样,您可以(几乎)在Quantity. 如果它不是数字,它将“吃掉”最后一个字符,但它不能删除不正确的字符之间的数字。

def changeLabel(*args): # require *args for trace
    #print "changeLabel", args

    custname_str = custname.get() 
    custname_int = 0 # default value when error

    try:
        custname_int = int( custname_str )
    except:
        if custname_str != "": # if not empty
            custname.set(custname_str[:-1]) # remove last char
            return 

    if 2 < custname_int <= 6:
        if relStatus.get() == 'HOSTS':
            label4Text.set("255.255.255.248")
        else: #if relStatus.get() == 'NETWORKS':
            label4Text.set("224.0.0.0")
    else:
        label4Text.set("-")
于 2013-11-27T04:50:08.250 回答