【问题标题】:Python - Tkinter & while loop at the same timePython - Tkinter 和同时循环
【发布时间】:2018-06-10 12:19:19
【问题描述】:

我是 python 初学者。我想知道是否有办法在运行 tkinter 窗口的同时运行循环。

from tkinter import *

root = Tk()
root.geometry("300x280")
root.title("Test")
Window=Frame(root,relief="raise", bg="#282d38")
Window.pack(side=TOP)

root.mainloop()

while True:
    print("hi")

我想在我的窗口运行时运行一个循环。

【问题讨论】:

  • 感谢您的超快速响应! :) 我对此很陌生。如何将此标记为已解决?
  • 我把它转换成答案,所以现在你可以:)
  • 一些你可以用root.after()而不是while循环或线程来做的事情。

标签: python loops tkinter while-loop


【解决方案1】:

线程是您所需要的。 https://www.tutorialspoint.com/python/python_multithreading.htm

例子:

import thread
import time

# Define a function for the thread
def print_time( threadName, delay):
   count = 0
   while count < 5:
      time.sleep(delay)
      count += 1
      print "%s: %s" % ( threadName, time.ctime(time.time()) )

# Create two threads as follows
try:
   thread.start_new_thread( print_time, ("Thread-1", 2, ) )
   thread.start_new_thread( print_time, ("Thread-2", 4, ) )
except:
   print "Error: unable to start thread"

while 1:
   pass

【讨论】:

  • 这个答案没有显示线程与 tkinter 的集成。
猜你喜欢
  • 2021-04-09
  • 1970-01-01
  • 1970-01-01
  • 2013-09-26
  • 2013-12-25
  • 1970-01-01
  • 1970-01-01
  • 2013-07-27
  • 1970-01-01
相关资源
最近更新 更多