是否可以通过python tkinter按钮控制或更改进程流。我有两节课。一个GuiClass和另一个ClientClass。我想通过按下开始按钮来启动该过程。并通过按停止按钮停止该过程。没有课程时,我知道该怎么做。但是作为GuiClass的ClientClass复合,我不知道该怎么做。我的过程是生成一些随机值。但是在以后的情况下,我将使用它每秒获取一些传感器测量数据,并且在GUI上显示这些数据。 (我之前问过同样的问题,没有任何答案。所以我简化了我的问题:)
class GuiClass:
def __init__(self, master, queue, endCommand, startCommand, stopCommand ):
self.queue = queue
self.radnom_value_var = tkinter.StringVar()
# Set up the GUI
Random_Value = tkinter.Label(master, text = "Value")
Random_Value.grid(row=2)
Random_Value_display = tkinter.Label(master, textvariable = self.radnom_value_var)
Random_Value_display.grid(row=4)
#buttons
console_end = tkinter.Button(master, text='Done', command=endCommand)
console_end.grid(row=11)
start_button = tkinter.Button(master, text='Start', command=startCommand)
start_button.grid(row=5)
stop_button = tkinter.Button(master, text='Stop', command=stopCommand)
stop_button.grid(row=6)
def processIncoming(self):
"""
Handle all the messages currently in the queue (if any).
"""
while self.queue.qsize():
try:
msg = self.queue.get(0)
# Check contents of q message
print (msg)
self.radnom_value_var.set(msg)
except queue.Empty:
pass
class ClientClass:
def __init__(self, master):
self.master = master
# Create the queue
self.queue = queue.Queue()
# Set up the GUI part
self.gui = GuiClass(master, self.queue, self.endApplication, self.start_Command, self.stop_Command)
self.start_status = 1 #-------do i need this? or what do i need here?
self.running = 1
self.thread1 = threading.Thread(target=self.workerThread1) # or can i create different thread based on button pressing?
self.thread1.start()
# Start the periodic call in the GUI to check if the queue contains
# anything
self.periodicCall()
def periodicCall(self):
if self.running==1: #because of this program start when i run the code. but i want is it wait for start button press
self.gui.processIncoming()
if self.running==0:
import sys
sys.exit(1)
self.master.after(1000, self.periodicCall)
def workerThread1(self):
while self.running==1:
time.sleep(1)
msg = rand.random()
self.queue.put(msg)
'''
i tried something like this. not successfull `
def workerThread1(self):
while self.start_status == 1 and self.running!=0 :
time.sleep(1)
msg = rand.random()
self.queue.put(msg)
print("start status ")
while self.stop_status = =1and self.running!=0:
time.sleep(1)
print("stop )
'''
def endApplication(self):
self.running = 0
def start_Command(self):
self.start_status = 1
def stopCommand(self):
self.stop_status = 1
rand = random.Random()
root = tkinter.Tk()
client = ClientClass(root)
root.mainloop()
答案 0 :(得分:0)
使用类确实并没有增加任何难度。该类的每个实例都应相互引用。
例如,您的ClientClass
已经传递了队列和主窗口小部件之类的东西。与其传递这些东西,不如传递它本身。然后,gui可以直接引用客户端的方法和标志。
例如:
class ClientClass():
def __init__(...):
...
self.gui = GuiClass(..., client=self)
...
class GuiClass:
def __init__(..., client):
self.client = client
...
console_end = tkinter.Button(..., command=self.client.endCommand)
start_button = tkinter.Button(..., command=self.client.startCommand)
stop_button = tkinter.Button(..., command=self.client.stopCommand)
通过上述操作,客户端将了解gui(self.gui
),而gui将了解客户端(self.client
)。这允许双向通信,因为每个实例都可以调用方法并从另一个实例获取属性。