我正在嘗試使用 tkinter,并嘗試制作一個程式,該程式可以讓用戶將一個值輸入到條目小部件中,通過計算處理該輸入值,然后能夠將其傳遞回 GUI 進行輸出。當我運行此代碼時,GUI 啟動但輸出小部件不執行任何操作。我已經為背景小部件設定了一種顏色,以確保它首先生成,并且確實顯示出來。
#TKinter Threading Test
#Importing all relevant tkinter modules
from tkinter import *
from tkinter import ttk
import tkinter as tk
from tkinter.ttk import *
#Importing threading
from threading import Thread
#Creating window
thrdng_root = tk.Tk()
thrdng_root.geometry("900x900")
#Creating grid columns
thrdng_root.columnconfigure(0, weight = 3)
thrdng_root.columnconfigure(1, weight = 7)
thrdng_root.columnconfigure(2, weight = 3)
#Creating grid rows
thrdng_root.rowconfigure(0, weight = 5)
thrdng_root.rowconfigure(1, weight = 1)
thrdng_root.rowconfigure(2, weight = 5)
thrdng_root.rowconfigure(3, weight = 5)
#Creating a variable for the input widget
input_var = StringVar()
#Creating entry widget for the user to input a value
input_widget = ttk.Entry(thrdng_root, textvariable = input_var)
input_widget.grid(column = 1, row = 0)
#Creating a variable for the output widget
output_var = StringVar()
#Crating the output widget
output_widget = ttk.Label(thrdng_root, textvariable = output_var, background = "#FFFFFF")
output_widget.grid(column = 1, row = 2)
#Defining the function after the global variables have been set so that they are recognised
def calculation_process():
#Making the loop go on indefinitely
loop_continue = "Yes"
while loop_continue == "Yes":
temp = str(input_var)
split_temp = temp.split("R")
calc_var = float(split_temp[1])
output_var = str(calc_var 1)
#Setting the function as a thread
calc_thread = Thread(target = calculation_process)
#Starting the thread so that the two indefinite loops can go on without blocking each other
calc_thread.start()
#Starting the indefinite GUI loop
thrdng_root.mainloop()
我正在嘗試做的事情是否可能,如果是,我錯過了什么?
uj5u.com熱心網友回復:
這是一種方法,使用queue.Queues。不需要使用StringVars,因為對于大多數小部件,使用它們自己的方法更容易。此外,所有這些都tkinter應該在一個行程和一個執行緒中運行,這意味著您不應該將它的任何“事物”放在另一個執行緒中或從其他執行緒呼叫它的任何方法(就像您嘗試做的那樣)。大部分解釋都在代碼注釋中:
import tkinter as tk
import threading
import queue
def send_value():
# get the value that user entered
value = entry.get()
# don't allow entering any new values
entry.config(state='disabled')
submit.config(state='disabled')
# put the value to do calculations with in the queue
queue_in.put(value)
def update_out():
# if there is a value in the output queue
if not queue_out.empty():
# get that value
value = queue_out.get()
# show the value on the label
output.config(text=value)
# set entry and button back to normal
entry.config(state='normal')
submit.config(state='normal')
# schedule repeated call to this function
root.after(100, update_out)
def calc():
while True:
if queue_in.empty():
continue
# if a value has been put in the input queue
value = queue_in.get()
# do calculations
new = value ' calculation :)'
# put the new value in the output queue
queue_out.put(new)
root = tk.Tk()
# create input and output queues
queue_in = queue.Queue()
queue_out = queue.Queue()
# start the calculation thread
threading.Thread(target=calc).start()
entry = tk.Entry(root)
entry.pack()
output = tk.Label(root)
output.pack()
# start the update "loop"
update_out()
submit = tk.Button(root, text='Calculate', command=send_value)
submit.pack()
root.mainloop()
有用:
queue模塊(內置)
另外:
我強烈建議*在匯入某些內容時不要使用通配符 ( ),您應該匯入您需要的內容,例如from module import Class1, func_1, var_2等等或匯入整個模塊:import module然后您也可以使用別名:import module as md或類似的東西,重點是不要除非您確實知道自己在做什么,否則不要匯入所有內容;名稱沖突是問題所在。
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/398971.html
