所以,我要做的是创建一个tkinter窗口,然后在屏幕上取消它,而无需重新打开GUI。我面临的第一个问题是线程似乎不起作用。程序选择只运行deplacewindow函数并忽略thread.start行下面的代码。有可能解决这个问题吗?或者是否有更好的方法可以在屏幕上移动tkinter窗口?
以下是代码:
import tkinter as tk
import time,threading
root = tk.Tk() # create a Tk root window
w = 200 # width for the Tk root
h = 200 # height for the Tk root
# get screen width and height
sw = root.winfo_screenwidth() # width of the screen
sh = root.winfo_screenheight() # height of the screen
# calculate x and y coordinates for the Tk root window to appear on the
# bottom right of the screen
x = sw - w - 20
y = sh - h - 80
print (sw,"-",w," ",sh,"-",h)
print (x,y)
# set the dimensions of the screen
# and where it is placed
root.geometry('%dx%d+%d+%d' % (w, h, x, y))
def mainloop():
global root
root.mainloop() # starts the mainloop
def deplaceWindow():
global root,x
print ("Starting loop")
while 1:
time.sleep(1)
x-=10
print (x)
#root.geometry('%dx%d+%d+%d' % (w, h, x, y))
root.geometry('%dx%d+%d+%d' % (w, h, x, y))
print ("starting thread")
threading.Thread(target=deplaceWindow()).start()
print ("starting mainloop")
mainloop()
答案 0 :(得分:0)
您想要的似乎是计时器事件。 TkInter使用 after 方法:
std::replace
此外:与GUI线程的任何交互都不能来自其他线程(如@Bryan_Oakley所解释的)。您可以在主脚本中使用root.after方法调度事件,但不能直接调用外部线程。