我想比较e1_var
和e2_var
中存储的两个值。如果它们是相同的,我想在主窗口上显示一个按钮;如果他们不匹配,我想显示另一个按钮。到目前为止我所尝试的并不起作用。有人可以告诉我该怎么做吗?
def lockscreen():
locks = Toplevel(width=500,height=500)
locks.title('Lock Screen')
l1 = Label(locks,text="Enter 4 Digit Lock Code")
l1.grid(row=1,column=1,columnspan=3)
e1=Entry(locks, bd=5)
e1.grid(row=2,column=1,columnspan=3)
e1_var=StringVar()
e1["textvariable"]=e1_var
def insert_1():
e1.insert(INSERT,1)
def insert_2():
e1.insert(INSERT,2)
def insert_3():
e1.insert(INSERT,3)
def insert_4():
e1.insert(INSERT,4)
def insert_5():
e1.insert(INSERT,5)
def insert_6():
e1.insert(INSERT,6)
def insert_7():
e1.insert(INSERT,7)
def insert_8():
e1.insert(INSERT,8)
def insert_9():
e1.insert(INSERT,9)
def insert_0():
e1.insert(INSERT,0)
def lock_action():
lock_code=e1_var.get()
print (lock_code)
Button(master,width=50,height=50, text='Unlock', command=unlock).place(x=0,y=0)
Button(locks, width=3, height=3, text='1', command =insert_1).grid(row=3,column=1)
Button(locks, width=3, height=3, text='2', command =insert_2).grid(row=3,column=2)
Button(locks, width=3, height=3, text='3', command =insert_3).grid(row=3,column=3)
Button(locks, width=3, height=3, text='4', command =insert_4).grid(row=4,column=1)
Button(locks, width=3, height=3, text='5', command =insert_5).grid(row=4,column=2)
Button(locks, width=3, height=3, text='6', command =insert_6).grid(row=4,column=3)
Button(locks, width=3, height=3, text='7', command =insert_7).grid(row=5,column=1)
Button(locks, width=3, height=3, text='8', command =insert_8).grid(row=5,column=2)
Button(locks, width=3, height=3, text='9', command =insert_9).grid(row=5,column=3)
Button(locks, width=3, height=3, text='Close').grid(row=6,column=1)
Button(locks, width=3, height=3, text='0', command =insert_0).grid(row=6,column=2)
block=Button(locks, width=3, height=3, text='Lock',command=lock_action)
block.grid(row=6,column=3)
def unlock():
ulocks = Toplevel(width=500,height=500)
ulocks.title('Unlock Screen')
l2 = Label(ulocks,text="Enter 4 Digit Unlock Code")
l2.grid(row=1,column=1,columnspan=3)
e2=Entry(ulocks, bd=5)
e2.grid(row=2,column=1,columnspan=3)
e2_var=StringVar()
e2["textvariable"]=e2_var
def insert_u1():
e2.insert(INSERT,1)
def insert_u2():
e2.insert(INSERT,2)
def insert_u3():
e2.insert(INSERT,3)
def insert_u4():
e2.insert(INSERT,4)
def insert_u5():
e2.insert(INSERT,5)
def insert_u6():
e2.insert(INSERT,6)
def insert_u7():
e2.insert(INSERT,7)
def insert_u8():
e2.insert(INSERT,8)
def insert_u9():
e2.insert(INSERT,9)
def insert_u0():
e2.insert(INSERT,0)
def unlock_action():
unlock_code=e2_var.get()
if lock_code==unlock_code
Button(master,width=800, height=800, text='IT WORKS!!!!!!!').place(x=400,y=0)
else if
Button(master,width=800,height=800, text='It doesnt work :-(').place(x=20,y=0)
Button(ulocks, width=3, height=3, text='1', command =insert_u1).grid(row=3,column=1)
Button(ulocks, width=3, height=3, text='2', command =insert_u2).grid(row=3,column=2)
Button(ulocks, width=3, height=3, text='3', command =insert_u3).grid(row=3,column=3)
Button(ulocks, width=3, height=3, text='4', command =insert_u4).grid(row=4,column=1)
Button(ulocks, width=3, height=3, text='5', command =insert_u5).grid(row=4,column=2)
Button(ulocks, width=3, height=3, text='6', command =insert_u6).grid(row=4,column=3)
Button(ulocks, width=3, height=3, text='7', command =insert_u7).grid(row=5,column=1)
Button(ulocks, width=3, height=3, text='8', command =insert_u8).grid(row=5,column=2)
Button(ulocks, width=3, height=3, text='9', command =insert_u9).grid(row=5,column=3)
Button(ulocks, width=3, height=3, text='Close').grid(row=6,column=1)
Button(ulocks, width=3, height=3, text='0', command =insert_u0).grid(row=6,column=2)
Button(ulocks, width=3, height=3, text='Unlock',command=unlock_action).grid(row=6,column=3)
master.mainloop()
答案 0 :(得分:0)
The Variable Classes (BooleanVar, DoubleVar, IntVar, StringVar):
get 方法以Python 对象 的形式返回变量的当前值。对于BooleanVar变量,返回值对于false为0,对于true为1。对于DoubleVar变量,返回的值是Python float。对于IntVar,它是一个整数。 对于 StringVar ,它可以是ASCII字符串或Unicode字符串,具体取决于内容。
第一个测试比较两个StringVar
个对象。由于每个都是它自己的实例,因此它们是唯一的。您必须进行深度比较才能确定相等。
第二个只是字符串比较。
from Tkinter import *
if __name__ == '__main__':
Tk()
e1_var = StringVar()
e2_var = StringVar()
print 'Test 1 [Object compare]: {}'\
.format('same' if e1_var == e2_var else 'different')
print 'Test 2 [String compare]: {}'\
.format('same' if e1_var.get() == e2_var.get() else 'different')
<强>输出:强>
Test 1 [Object compare]: different
Test 2 [String compare]: same