我已经创建了一个IntVar()this.v。当this.v=1时,我的elif语句为true,但事实并非如此。我做错了什么?当我打印(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)
elif
将始终触发,因为变量不能同时是!= 1
和!=2
!
这不是"语义错误",而是程序员的逻辑错误。
当你说"如果x不是1,就这样做,或者如果x不是2,就这么做"时,它将始终运行。考虑以下值:
- 0(零):不是1,因此elif测试通过
- 1(一):是1,但不是2,因此elif测试通过
- 2(二):不是1,所以elif测试通过
- 3(三):不是1,所以elif测试通过