在Python 2.7中需要帮助Tkinter

时间:2013-11-27 02:00:08

标签: python tkinter label

我正在用Python写一个子网划分程序,我遇到了一个问题。

到目前为止,一切都在减少一件事。我不知道如何更改方法中的标签。在下面的代码中,SubnetM是用于显示子网掩码的变量。它默认设置为0,但是当您选择HOSTS并输入6作为Quantity时。 0不会更改为255.255.255.248。请帮助

from Tkinter import *

SubnetM = 0

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

def changeLabel():
    if radio1 == 'HOSTS':
        if Quantity == 6:
            SubnetM = "255.255.255.248"
            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)
radio1 = Radiobutton(app, text="HOSTS", value="HOSTS", variable=relStatus, command=beenclicked).pack()
radio1 = Radiobutton(app, text="NETWORKS", value="NETWORKS", variable=relStatus, command=beenclicked).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 :(得分:1)

要解决您的问题,请按以下方式设置changeLabel

def changeLabel():
    # Get the radiobutton's StringVar and see if it equals "HOSTS"
    if relStatus.get() == 'HOSTS':
        # Get the entrybox's IntVar and see if it equals 6
        if custname.get() == 6:
            # Set the label's StringVar to "255.255.255.248"
            label4Text.set("255.255.255.248")

此外,Tkinter小部件的.pack方法返回None。所以,你应该制作定义无线电按钮的部分,如下所示:

radio1 = Radiobutton(app, text="HOSTS", value="HOSTS", variable=relStatus, command=beenclicked)
radio1.pack()
radio2 = Radiobutton(app, text="NETWORKS", value="NETWORKS", variable=relStatus, command=beenclicked)
radio2.pack()