在Python 2.5中需要有关Entry Tkinter的帮助

时间:2013-11-27 04:05:37

标签: python tkinter tkinter-entry

当您在字段中输入时,有没有办法让Tkinter中的条目做出反应?我希望此功能适用于代码的数量部分。

单击radiobuttons时会自动生成label4text,但输入Quantity时不会生成。

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

1 个答案:

答案 0 :(得分:2)

trace()更改StringVar()后,changeLabel可以custname使用custname.trace("w", changeLabel)

custname

我将IntVarStringVar更改为Entry,因为*args在空(无数字)时将值转换为int时出现问题

我将changeLabel()添加到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() 会向函数发送一些参数。

完整代码:

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

仅在Python 2.7上测试

修改

我添加了一些修改:

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("-") 中的数字。如果它不是数字,它将“吃掉”最后一个字符,但它不能删除不正确的字符betwin数字。

{{1}}