问题描述
我想为我的项目实现一个非常简单的GUI。我以前只是使用Print语句来输出一些文本和数据。但是,这并不是很方便,而且由于一个人需要操作我正在编码的设备,因此他需要清楚地看到我将要在GUI上显示的指令。
我的代码:
main()
myConnection = MysqL.connector.connect( host=hostname,user=username,passwd=password,db=database )
counter = 0
window = tk.Tk()
window.title("GUI")
window.geometry("400x200")
while(1):
# OPERACIJOS KODAI:
# 0 - PILDYMAS
# 1 - KOMPLEKTAVIMAS
# 2 - NETINKAMAS KODAS
tk.Label(window,text = "Scan barcode here:").pack()
entry = tk.Entry(window)
entry.pack()
var = tk.Intvar()
button = tk.Button(window,text="Continue",command = lambda: var.set(1))
button.pack()
print("waiting...")
button.wait_variable(var)
result = entry.get()
print("Entry string=",result)
var.set(0)
operacijos_kodas=Scanning_operation(myConnection,result)
print("operacijos kodas=",operacijos_kodas)
if(operacijos_kodas == 0):
tk.label(window,text = "PILDYMO OPERACIJA:").pack()
pildymo_operacija(myConnection)
elif(operacijos_kodas == 1):
tk.Label(window,text = "PAKAVIMO OPERACIJA:").pack()
insertData_komplektacija(myConnection,"fmb110bbv801.csv");
update_current_operation(myConnection);
picking_operation();
elif(operacijos_kodas == 2):
print("Skenuokite dar karta")
#break
window.mainloop();
首先,我不确定应该在哪里调用函数window.mainloop()
。
第二,由于我的系统在无限的while循环中运行(操作在用户扫描条形码时开始,然后他完成操作,而while循环又重新开始(等待用户扫描条形码)因此,我只需要显示一些文本并允许用户在文本框中输入数据即可。
有人可以建议我该GUI是否适合我的需求,还是应该寻找替代方案?
更新*********************
我尝试使用mainloop:
print ("Using MysqL.connector…")
main()
GPIO_SETUP()
myConnection = MysqL.connector.connect( host=hostname,db=database )
counter = 0
window = tk.Tk()
window.resizable(False,False)
window_height = 1000
window_width = 1200
#window.attributes('-fullscreen',True)
#window.config(height=500,width=500)
#can = Canvas(window,bg='red',height=100,width=100)
#can.place(relx=0.5,rely=0.5,anchor='center')
window.title("GUI")
screen_width = window.winfo_screenwidth()
screen_height= window.winfo_screenheight()
x = int((screen_width/ 2) - (window_width / 2))
y = int((screen_height/ 2) - (window_height / 2))
window.geometry("{}x{}+{}+{}".format(window_width,window_height,x,y))
label1=Label(window,text = "SKENUOKITE BARKODA(GUID) ARBA DAIKTO RIVILINI KODA:")
label1.pack()
entry = Entry(window)
entry.pack()
var = tk.Intvar()
button = Button(window,text="Testi operacija",width = 30,command = lambda: var.set(1))
button.pack()
#button2 = Button(window,text="RESTARTUOTI SIstemA",command = restart_devices())
#button2.pack()
print("waiting...")
button.wait_variable(var)
Scanned_serial = entry.get()
print("Entry string=",Scanned_serial)
var.set(0)
label2=Label(window,text = "Vykdoma operacija:")
label2.pack()
window.update()
window.after(1000,Full_operation(Scanned_serial,label2,window))
window.mainloop()
这是我的代码。如你看到的。我先调用Full_operation函数,然后调用window.mainloop()
我的全面手术:
def Full_operation(Scanned_serial,window):
operacijos_kodas=Scanning_operation(myConnection,Scanned_serial)
print("operacijos kodas=",operacijos_kodas)
if(operacijos_kodas == 0):
label2.config(text = "SPAUSKITE MYGTUKA ANT DEZES KURIA norITE PILDYTI:")#update the label2
window.update()#call update to update the label
pildymo_operacija(myConnection,Scanned_serial,window)
elif(operacijos_kodas == 1):
insertData_komplektacija(myConnection,"fmb110bbv801.csv");
update_current_operation(myConnection);
#label2.config(text = "IMKITE DAIKTUS IS ZALIOS DEZUTES:")#update the label2
picking_operation(myConnection,label2);
elif(operacijos_kodas == 2):
print("Skenuokite dar karta")
label2.config(text = "NUSKENUOTAS NEgalIMAS KODAS:")#update the label2
window.update()#call update to update the label
如何确保每次进入FUll_operation函数时,都从干净的GUI重新启动并开始其他操作。
现在我可以完成一次操作。在那之后,GUI没有响应。 我在full_operation的开头添加了一条print语句,并且一旦完成一次它就不会执行,因此我的主窗口似乎无法正常工作。
解决方法
您需要调整代码以使用GUI。您不能在不引起各种问题的情况下将无限循环引入tkinter GUI。
Mainloop只能调用一次。
我建议您将所有扫描/保存操作移至单独的功能中,并计划使用tkinter after
方法定期进行此功能。
例如,如果您调用函数scan
,则可以将其安排为使用1秒后发生
root.after(1000,scan)
一种更高级的方法是让您的扫描代码在单独的线程上运行。
此外,您当前每次尝试遍历while循环时都试图创建标签,而不是仅创建和打包一次并在执行“扫描”时更新标签的文本。您可以使用config方法来更新标签的文本,例如
## Create a label
label1 = tk.Label(window,text = "PAKAVIMO OPERACIJA:")
##Pack the label
label1.pack()
## Update the text later
label1.config(text="New Text")
这是从功能定期更新tkinter小部件的示例。
import tkinter as tk
import random
def scanning():
num = random.randint(0,100)
entryTemperature.delete(0,tk.END) #Delete the current contents
entryTemperature.insert(0,f"{num} K") #Add new text
root.after(1000,scanning) #Schedule the function to run again in 1000ms (1 second)
root = tk.Tk()
entryTemperature = tk.Entry(root)
entryTemperature.grid(padx=50,pady=50)
root.after(1000,scanning)
root.mainloop()