如何正确更改单选按钮的值?

How do I properly change the values of a Radiobutton?

我创建了一个 IntVar() this.v。 当this.v = 1时,我的elif语句为真,不应该这样。 我做错了什么? 当我 print(this.v.get()) 时,返回的值为 1。 (导入 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)

当你说 "do this if x is not 1, OR do this if x is not 2" 时,它总是 运行。考虑以下值:

  • 0(零):不是1,所以elif测试通过
  • 1(一):是一但不是2,所以elif测试通过
  • 2(二):不是1,所以elif测试通过
  • 3(三):不是1,所以elif测试通过

elif会一直触发,因为一个变量不能同时!= 1!=2

这不是"semantic error",而是程序员的逻辑错误。