如何在Python3.4中并发运行多个线程?网格布局管理器如何处理控件?
How to run multiple threads concurrently in Python3.4? And how does the Grid Layout Manager handle the controls?
def udp_communication_loop():
global state
global countdown_val
global countdown
_socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
_socket.bind(("", 1200))
while 1:
received_message = ""
received_message, addr = _socket.recvfrom(1024)
received_message = str(received_message)
if received_message != "":
if received_message.find("Start") != -1:
if(state == None):
state = 12
countdown_val = int(received_message[7:-1])
countdown = 1
cd_thread = threading.Thread(target=start_countdown())
cd_thread.daemon = True
cd_thread.start()
if received_message.find("Stop") != -1
countdown = -1
这是我作为主程序的新线程启动的函数,它侦听 UDP 包并根据消息启动或停止在 GUI 上显示的计时器。
如果 UDP 包包含消息 "Start",它会在新线程中开始倒计时。
def start_countdown():
global countdown
global countdown_val
while(countdown != -1):
global state
if state == 11:
Label(root, text="AB", fg="red", relief=RIDGE,font=("Arial", 100)).grid(row=1, column=0, sticky="nsew")
Label(root, text="CD", relief=RIDGE,font=("Arial", 100)).grid(row=1, column=1, sticky="nsew")
state = 12
elif state == 12:
Label(root, text="AB", fg="red", relief=RIDGE,font=("Arial", 100)).grid(row=1, column=0, sticky="nsew")
Label(root, text="CD", relief=RIDGE,font=("Arial", 100)).grid(row=1, column=1, sticky="nsew")
state = 21
elif state == 21:
Label(root, text="CD", fg="red", relief=RIDGE,font=("Arial", 100)).grid(row=1, column=1, sticky="nsew")
Label(root, text="AB", relief=RIDGE,font=("Arial", 100)).grid(row=1, column=0, sticky="nsew")
state = 22
elif state == 22:
Label(root, text="CD", fg="red", relief=RIDGE,font=("Arial", 100)).grid(row=1, column=1, sticky="nsew")
Label(root, text="AB", relief=RIDGE,font=("Arial", 100)).grid(row=1, column=0, sticky="nsew")
state = 11
cd2 = countdown_val
print("Test before while ",countdown)
while(cd2 >= 0) and (countdown != -1):
print("test")
Label(root, text=cd2, relief=RIDGE, font=("Arial", 250)).grid(row=0, column=0, columnspan=2, sticky="nsew")
print(cd2)
cd2 = cd2-1
if(cd2 == -1):
countdown = -1
time.sleep(1)
只要全局倒计时变量不是-1,此函数就会倒计时直到它达到 0。
这是我的问题:我发出开始信号,倒计时开始。然后在倒计时时我发送停止信号但计时器不会停止。我尝试在 udp_communication_loop 中打印消息,但它只在倒计时结束后打印消息,这让我觉得我的两个线程不是同时 运行。即使倒计时,我也想继续收听 UDP 包。如何实现?
与同一程序相关的另一个问题是关于网格布局管理器的工作原理。
我声明了一个名为计时器的标签。这是显示计时器的那个。但是我无法从我的函数中访问它。我尝试将它作为参数通过线程传递并将其声明为全局变量但是当我尝试 timer.config(text=...) 时,我收到错误:"None type variable doesn't have a method called config"。因此,我没有为我已经存在的计时器标签设置新文本,而是每次都在同一网格单元格中创建一个包含不同文本的新文本。我一直想知道的是,网格布局会破坏同一单元格中包含的先前标签还是只是叠加它们?为什么我一开始就无法访问我的计时器标签?
提前致谢
cd_thread = threading.Thread(target=start_countdown())
这一行就是问题所在。将 start_countdown() 紧接在 cd_thread.start.
之前启动函数
cd_thread = threading.Thread(target=start_countdown)
这解决了我的问题。
只是我的语法错误。
def udp_communication_loop():
global state
global countdown_val
global countdown
_socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
_socket.bind(("", 1200))
while 1:
received_message = ""
received_message, addr = _socket.recvfrom(1024)
received_message = str(received_message)
if received_message != "":
if received_message.find("Start") != -1:
if(state == None):
state = 12
countdown_val = int(received_message[7:-1])
countdown = 1
cd_thread = threading.Thread(target=start_countdown())
cd_thread.daemon = True
cd_thread.start()
if received_message.find("Stop") != -1
countdown = -1
这是我作为主程序的新线程启动的函数,它侦听 UDP 包并根据消息启动或停止在 GUI 上显示的计时器。
如果 UDP 包包含消息 "Start",它会在新线程中开始倒计时。
def start_countdown():
global countdown
global countdown_val
while(countdown != -1):
global state
if state == 11:
Label(root, text="AB", fg="red", relief=RIDGE,font=("Arial", 100)).grid(row=1, column=0, sticky="nsew")
Label(root, text="CD", relief=RIDGE,font=("Arial", 100)).grid(row=1, column=1, sticky="nsew")
state = 12
elif state == 12:
Label(root, text="AB", fg="red", relief=RIDGE,font=("Arial", 100)).grid(row=1, column=0, sticky="nsew")
Label(root, text="CD", relief=RIDGE,font=("Arial", 100)).grid(row=1, column=1, sticky="nsew")
state = 21
elif state == 21:
Label(root, text="CD", fg="red", relief=RIDGE,font=("Arial", 100)).grid(row=1, column=1, sticky="nsew")
Label(root, text="AB", relief=RIDGE,font=("Arial", 100)).grid(row=1, column=0, sticky="nsew")
state = 22
elif state == 22:
Label(root, text="CD", fg="red", relief=RIDGE,font=("Arial", 100)).grid(row=1, column=1, sticky="nsew")
Label(root, text="AB", relief=RIDGE,font=("Arial", 100)).grid(row=1, column=0, sticky="nsew")
state = 11
cd2 = countdown_val
print("Test before while ",countdown)
while(cd2 >= 0) and (countdown != -1):
print("test")
Label(root, text=cd2, relief=RIDGE, font=("Arial", 250)).grid(row=0, column=0, columnspan=2, sticky="nsew")
print(cd2)
cd2 = cd2-1
if(cd2 == -1):
countdown = -1
time.sleep(1)
只要全局倒计时变量不是-1,此函数就会倒计时直到它达到 0。
这是我的问题:我发出开始信号,倒计时开始。然后在倒计时时我发送停止信号但计时器不会停止。我尝试在 udp_communication_loop 中打印消息,但它只在倒计时结束后打印消息,这让我觉得我的两个线程不是同时 运行。即使倒计时,我也想继续收听 UDP 包。如何实现?
与同一程序相关的另一个问题是关于网格布局管理器的工作原理。
我声明了一个名为计时器的标签。这是显示计时器的那个。但是我无法从我的函数中访问它。我尝试将它作为参数通过线程传递并将其声明为全局变量但是当我尝试 timer.config(text=...) 时,我收到错误:"None type variable doesn't have a method called config"。因此,我没有为我已经存在的计时器标签设置新文本,而是每次都在同一网格单元格中创建一个包含不同文本的新文本。我一直想知道的是,网格布局会破坏同一单元格中包含的先前标签还是只是叠加它们?为什么我一开始就无法访问我的计时器标签?
提前致谢
cd_thread = threading.Thread(target=start_countdown())
这一行就是问题所在。将 start_countdown() 紧接在 cd_thread.start.
之前启动函数cd_thread = threading.Thread(target=start_countdown)
这解决了我的问题。
只是我的语法错误。