How do I properly change the values of a Radiobutton?

49 Views Asked by At

I have created a IntVar() this.v. When this.v = 1, my elif statement is true, which should not be the case. What have I done wrong? When I print(this.v.get()) the value returned is 1. (import tkinter)

this.v = IntVar()
this.button1 = Radiobutton(this.root,text = "Small Boxes First",variable = this.v,value = 1)
this.button1.grid(row = 2,column = 5)
this.button2 = Radiobutton(this.root,text = "Large Boxes First",variable = this.v,value = 2)
this.button2.grid(row = 3,column = 5)

def packNSaveClicked(this):
        if(int(this.wid.get()) <= 0 or int(this.len.get()) <= 0 or this.len.get() == '' or this.wid.get() == ''):
            messagebox.showerror("Truck Size Error", "The length or width of the Truck is not a valid value!")
        elif(int(this.v.get()) != 1 or int(this.v.get()) != 2):
           #ALWAYS SHOWING UP, even though print statement prints out 1 or 2
            print(this.v.get())
            messagebox.showerror("Packing Error", "Pack algorithm not selected!")
        else:
          ...(this code not relevant)
2

There are 2 best solutions below

0
too honest for this site On BEST ANSWER

The elif will always trigger, as a variable cannot be != 1 and !=2 at the same time!

That is not a "semantic error", but a logical error by the programmer.

0
Bryan Oakley On

When you say "do this if x is not 1, OR do this if x is not 2", it will always run. Consider the following values:

  • 0 (zero): it is not 1, so the elif test passes
  • 1 (one): it is one but it is it is not 2, so the elif test passes
  • 2 (two): it is not 1, so the elif test passes
  • 3 (three): it is not 1, so the elif test passes