[英]Trying to Exit a While True Loop using PySimpleGUI
主要.py:
import PySimpleGUI as sg
import Config
import threading
def main():
layout = [ [sg.Text('Real Time Raspberry Pi Sniffer')],
[sg.Button('Run While Loop'), sg.Button('Exit')], # a couple of buttons
[sg.Output(size=(60,15))] ] # an output area where all print output will go
#[sg.Input(key='_IN_')] ] # input field where you'll type command
window = sg.Window('Realtime Shell Command Output', layout)
while True: # Event Loop
event, values = window.Read()
if event == 'Run While Loop':
t1 = threading.Thread(target = Config.whileLoop())
t1.start()
elif event == 'Exit' or event == WIN_CLOSED:
print('CLICKED EXIT')
window.Close()
if __name__ == '__main__':
main()
配置文件
from PySimpleGUI.PySimpleGUI import WIN_CLOSED
def whileLoop():
state = True
while (state == True):
print("It works!")
我正在嘗試創建在用戶單擊按鈕時運行 while 循環的 GUI 窗口(在這種情況下,當他們單擊“Run While Loop”時)。 但是,我遇到了一個問題,因為我的代碼卡在了 Config.py 中的嵌套 while 循環中。 我希望代碼能夠退出 while 循環並在單擊“退出”按鈕時由程序停止。 我研究了線程,不知道還能做什么。 有什么幫助,謝謝!
我導入在Config.py
中定義的類Func
,然后調用實例Func()
的方法while_loop
。 為 while 循環是否繼續運行設置一個標志 True 或 False。
示例代碼
# Config.py
from time import sleep
from PySimpleGUI import WIN_CLOSED
class Func():
def __init__(self):
self.state = False
self.count = 0
def while_loop(self, window):
while self.state:
sleep(0.5) # Simulate job done here
self.count += 1
window.write_event_value("Done", self.count) # update GUI by event
# main.py
from time import sleep
from threading import Thread
import PySimpleGUI as sg
import Config
def main():
layout = [
[sg.Text('Real Time Raspberry Pi Sniffer')],
[sg.Button('Start'), sg.Button('Stop'), sg.Button('Exit')],
[sg.StatusBar('', size=60, key='Status')],
]
window = sg.Window('Realtime Shell Command Output', layout, enable_close_attempted_event=True)
status = window['Status']
func = Config.Func()
thread = None
while True:
event, values = window.read()
if event in (sg.WINDOW_CLOSE_ATTEMPTED_EVENT, 'Exit'):
func.state = False
sleep(0.5) # Wait thread to stop
break
elif event == 'Start' and thread is None:
func.state = True
func.count = 0
thread = Thread(target=func.while_loop, args=(window,), daemon=True)
thread.start()
elif event == 'Stop':
func.state = False
thread = None
elif event == 'Done':
count = values[event]
status.update(f"Job done at #{count:0>3}")
window.Close()
if __name__ == '__main__':
main()
這是一個猜測,因為我之前沒有做過你正在嘗試的事情。
也許如果你改變你的 config.py 是這樣的:
state=True
def whileLoop():
while (state == True):
print("It works!")
(使 state 成為模塊全局標志)
並修改您的事件循環,如:
while True: # Event Loop
event, values = window.Read()
if event == 'Run While Loop':
t1 = threading.Thread(target = Config.whileLoop)
t1.start()
elif event == 'Exit' or event == WIN_CLOSED:
print('CLICKED EXIT')
Config.state = False # set the exit flag
t1.join() # I'm not sure about this, could try without
window.Close()
同樣,我已經使用 Python 很長時間了,但從來沒有這樣。
就在這里: t1 = threading.Thread(target = Config.whileLoop())
您在設置線程變量時調用了 Config.whileLoop()。 您必須改為t1 = threading.Thread(target = Config.whileLoop)
它實際上並沒有調用目標函數Config.whileLoop
。 t1.start()
實際上會調用它。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.