我正在构建一个GUI,该GUI首先接受来自用户的输入数字并使对应的按钮变为绿色。现在,我运行另一个python脚本,该脚本扫描apriltag(某种QR码)并返回ID,并将其写入到位于同一文件夹中的文件中。我所附的代码包括各个位置的打印语句(“ Done”,“ phew”,“ haha”)。在运行GUI时,首先打印Done命令,然后是phew和haha(多次)。但是,按钮不会将其颜色更改为绿色,而仅在相机检测到apriltag时才更改其颜色。该代码已附加。当我在GUI中按“确定”按钮时,将调用该函数。
我试图包括延迟,也试图使用线程。但是这些方法都不起作用。
import Tkinter
def action3():
f = open("test.txt","r")
data = f.read()
y = int(str(data))
f.close()
f = open("test.txt","w")
f.write("0")
f.close()
x = int(e.get())
f = open("test.txt","r")
data = f.read()
y = int(str(data))
f.close()
print("Done")
if (x == 1):
b1['bg'] = 'green'
b2['bg'] = 'white'
b3['bg'] = 'white'
b4['bg'] = 'white'
print("phew")
#time.sleep(2)
elif (x == 2):
b1['bg'] = 'white'
b2['bg'] = 'green'
b3['bg'] = 'white'
b4['bg'] = 'white'
print("phew")
elif (x == 3):
b1['bg'] = 'white'
b2['bg'] = 'white'
b3['bg'] = 'green'
b4['bg'] = 'white'
print("phew")
elif (x == 4):
b1['bg'] = 'white'
b2['bg'] = 'white'
b3['bg'] = 'white'
b4['bg'] = 'green'
print("phew")
#time.sleep(5)
while(y==0): ##Update y
f = open("test.txt","r")
data = f.read()
y=int(str(data))
print("Haha")
f.close()
time.sleep(2)
##Code for GUI
b1 = Button(window, text="Box1", bg='white', width=30, height=13)
b1.grid(row=1, sticky=W)
b2 = Button(window, text="Box2", bg = 'white', width = 30,height = 13)
b2.grid(row=2, sticky=W)
b3 = Button(window, text="Box3", bg = 'white', width = 30,height = 13)
b3.grid(row=1, column=1, sticky=W)
b4 = Button(window, text="Box4", bg = 'white', width = 30,height = 13)
b4.grid(row=2, column=1, sticky=W)
bget = Button(window, text="OK", bg='pink', width = 10,height =
2,command=action3)
bget.grid(row=3, column=0, sticky=E)
e = Entry(window)
e.grid(row=3, column=1)
window.mainloop()