我正在測驗 tkinter 并嘗試對程序中創建的 GUI 中的值進行計算。如果 GUI 創建不在程序中,則它作業正常,但如果小部件是在程序中制作的,則無法識別小部件,即使 GUI 程序在其他函式/程序之前定義并在之后呼叫。這是不可能的還是我做錯了什么?
#TKinter Threading Test
#Importing all relevant tkinter modules
import tkinter as tk
from tkinter import ttk
#Importing threading
from threading import Thread
#Importing queues
import queue
def take_value():
#Place the input_widget value into the queue
temp = input_widget.get()
print(temp)
#Temporarily pause the entry of new variables
input_widget.config(state = "disabled")
output_widget.config(state = "disabled")
queue_input.put(temp)
def place_value():
#Check if there is a value in the output queue at all
if queue_output.empty():
#print("Output queue is empty")
temp = "Nothing"
else:
temp = queue_output.get()
#Place the value onto the label
output_widget.config(text = temp)
#Re-activate the widgets
input_widget.config(state = "normal")
output_widget.config(state = "normal")
thrdng_root.after(100, place_value)
def calculation_process():
#Check if there is a value in the input queue at all
while True:
if queue_input.empty():
#print("Input queue is empty")
temp = "Nothing"
else:
temp = queue_input.get()
new_value = str(float(temp) 1)
print(new_value)
queue_output.put(new_value)
def create_GUI():
#Creating window
thrdng_root = tk.Tk()
thrdng_root.geometry("900x900")
#Creating a queue for values going into the calculation
queue_input = queue.Queue()
#Creating a queue for values coming out
queue_output = queue.Queue()
#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)
thrdng_root.rowconfigure(4, weight = 5)
#Creating entry widget for the user to input a value
input_widget = ttk.Entry(thrdng_root)
input_widget.grid(column = 1, row = 0)
#Creating the output widget
output_widget = ttk.Label(thrdng_root, background = "#FFFFFF")
output_widget.grid(column = 1, row = 2)
place_value()
#Creating the button that submits the data to the loop
submit_widget = ttk.Button(thrdng_root, text = "Calculate", command = take_value)
submit_widget.grid(column = 1, row = 3)
#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()
create_GUI()
uj5u.com熱心網友回復:
這對于 tkinter 小部件和任何其他 python 物件沒有什么不同。您需要保存對小部件的參考,并提供一種訪問該參考的方法。這通常通過以下方法之一完成:
- 如果您不使用類,請將參考保存為全域
- 如果您使用類,請將參考保存為實體變數
- 將參考傳遞給需要它的函式。
由于您沒有使用類,因此消除了#2。最簡單的解決方案是使用全域變數。對于您希望能夠在其創建位置以外的函式中訪問的每個小部件,您應該將其宣告為全域。
例如,如果input_widget
是您需要在多個地方使用的那些小部件之一,請在定義它的函式中將其宣告為全域。
def create_GUI():
global input_widget
...
input_widget = ttk.Entry(thrdng_root)'
...
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/398951.html
上一篇:當我嘗試在VsCode上使用Tkinter時,我收到一個NameError,提示名稱“Tk”未定義
下一篇:放置包含按鈕的框架