Kraig Clubb
Kraig Clubb

Reputation: 87

Need assistance Tkinter in Python 2.7

I am writing a subnetting program in Python and I have come across a problem.

So far everything is working minus one thing. I dont know how to change a label in a method. in the code below, SubnetM is the variable being used to show the subnet mask. It is set to 0 by default but when you select HOSTS and enter 6 as Quantity. The 0 does not change to 255.255.255.248. PLEASE HELP

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

Upvotes: 0

Views: 85

Answers (1)

user2555451
user2555451

Reputation:

To fix your problem, make changeLabel like this:

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

Also, the .pack method of a Tkinter widget returns None. So, you should make the part that defines the radiobuttons like this:

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

Upvotes: 1

Related Questions