Python - Tkinter& while while同时循环

时间:2018-06-10 12:19:19

标签: python loops tkinter while-loop

我是一名蟒蛇初学者。我想知道是否有办法在运行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")

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

1 个答案:

答案 0 :(得分:-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