当前位置: 首页 > 面试题库 >

While循环锁定应用程序

宰父疏珂
2023-03-14
问题内容

在我正在开发的应用程序上,我已经花了好一阵子了。经过数小时的尝试,调试了一个问题,该问题是接口锁定,什么也没发生,我发现这是可怕的While循环。请参阅下面的示例并运行它。通过单击按钮启动while循环时,您将无法在屏幕上执行任何其他操作。在这种情况下,只需按下一个简单的警报按钮即可。

from Tkinter import *
import tkMessageBox

root = Tk()
root.geometry("450x250+300+300")
root.title("Raspberry PI Test")

def myloop():
    count = 0
    while (count < 500):
       print 'The count is:', count
       count = count + 1

    print "Good bye!"

def mymessage():
    tkMessageBox.showinfo(title="Alert", message="Hello World!")

buttonLoop = Button(root, text="Start Loop", command=myloop)
buttonLoop.place(x=5, y=15)

buttonMessage = Button(root, text="Start Loop", command=mymessage)
buttonMessage.place(x=85, y=15)


root.mainloop()

如何有一个循环需要运行直到计数完成并且仍然能够在我的应用程序中执行其他任务?我还应该注意,我已经使用Thread尝试过同样的事情,这没关系。UI仍在等待While循环结束,然后才能执行任何操作。


问题答案:

现在,我了解了您想要更好的东西(秒表),我建议使用root.after命令

from Tkinter import *
import tkMessageBox
import threading
import time
root = Tk()
root.geometry("450x250+300+300")
root.title("Raspberry PI Test")
print dir(root)
count = 0
def start_counter():
    global count
    count = 500
    root.after(1,update_counter)
def update_counter():
    global count
    count -= 1
    if count < 0:
        count_complete()
    else:
        root.after(1,update_counter)

def count_complete():
    print "DONE COUNTING!! ... I am now back in the main thread"
def mymessage():
    tkMessageBox.showinfo(title="Alert", message="Hello World!")

buttonLoop = Button(root, text="Start Loop", command=myloop)
buttonLoop.place(x=5, y=15)

buttonMessage = Button(root, text="Start Loop", command=mymessage)
buttonMessage.place(x=85, y=15)


root.mainloop()

(以下为原始答案)

使用线程

from Tkinter import *
import tkMessageBox
import threading
import time
root = Tk()
root.geometry("450x250+300+300")
root.title("Raspberry PI Test")
print dir(root)
def myloop():
    def run():
        count = 0
        while (count < 500) and root.wm_state():
           print 'The count is:', count
           count = count + 1
           time.sleep(1)

        root.after(1,count_complete)
    thread = threading.Thread(target=run)
    thread.start()
def count_complete():
    print "DONE COUNTING!! ... I am now back in the main thread"
def mymessage():
    tkMessageBox.showinfo(title="Alert", message="Hello World!")

buttonLoop = Button(root, text="Start Loop", command=myloop)
buttonLoop.place(x=5, y=15)

buttonMessage = Button(root, text="Start Loop", command=mymessage)
buttonMessage.place(x=85, y=15)


root.mainloop()

请注意,当您显示将在Windows api级别阻塞的信息框时,线程计数将一直等到关闭为止…为了解决该问题,您可以将线程替换为多处理,我认为



 类似资料:
  • 只要给定条件为真,Perl编程语言中的while循环语句就会重复执行目标语句。 语法 (Syntax) Perl编程语言中while循环的语法是 - while(condition) { statement(s); } 这里的statement(s)可以是单个陈述或一个陈述块。 condition可以是任何表达。 当条件为真时,循环迭代。 当条件变为假时,程序控制将立即传递到循环之后的行。

  • 编写程序时,您可能会遇到需要反复执行操作的情况。 在这种情况下,您需要编写循环语句以减少行数。 JavaScript支持所有必要的循环,以减轻编程压力。 while循环 JavaScript中最基本的循环是while循环,将在本章中讨论。 while循环的目的是只要expression为真,就重复执行语句或代码块。 表达式变为false,循环终止。 流程图 while loop流程图如下 - 语法

  • 只要给定条件为真,Objective-C编程语言中的while循环语句就会重复执行目标语句。 语法 (Syntax) Objective-C编程语言中while循环的语法是 - while(condition) { statement(s); } 这里, statement(s)可以是单个语句或语句块。 condition可以是任何表达式,true是任何非零值。 当条件为真时,循环迭代。

  • While循环一次又一次地执行相同的代码,直到满足停止条件。 语法 (Syntax) 在R中创建while循环的基本语法是 - while (test_expression) { statement } 流程图 (Flow Diagram) 这里while循环的关键点是循环可能永远不会运行。 当测试条件并且结果为假时,将跳过循环体并且将执行while循环之后的第一个语句。 例子 (Exam

  • 在给定条件为真时重复语句或语句组。 它在执行循环体之前测试条件。 只要给定条件为真, while循环语句就会重复执行目标语句。 语法 (Syntax) 以下是while循环的语法。 while(condition){ statement(s); } 这里, statement(s)可以是单个语句或语句块。 condition可以是任何表达式,true是任何非零值。 当条件为真时,循环迭代。

  • 只要给定条件为真,Swift 4编程语言中的while循环语句就会重复执行目标语句。 语法 (Syntax) Swift 4编程语言中while循环的语法是 - while condition { statement(s) } 这里的statement(s)可以是单个陈述或一个陈述块。 condition可以是任何表达。 当条件为真时,循环迭代。 当条件变为假时,程序控制传递到紧接循环之后