[英]How to move tkinter windows using python after it has been created?
I am looking to move a tkinter window to different x and y coordinates after it has been created. 创建后,我希望将tkinter窗口移动到不同的x和y坐标。 For example 5 seconds after the window has been created move it to
例如,创建窗口后5秒钟,将其移至
x=??? X = ??? and y=???.
和y = ???。
from tkinter import*
root = Tk()
root.title("lol")
root.geometry("300x300")
photo = PhotoImage(file="pepe.gif")
label = Label(image=photo).place(x=10, y=10)
root.mainloop()
The above code is just an example, how does it need to be modified to move by itself? 上面的代码仅是示例,如何对其进行修改以自行移动?
This example will show you how to reset the root geometry that controls the position of the window on screen; 本示例将向您展示如何重设控制窗口在屏幕上位置的根几何。 clicking on the image will toggle the locationof the window.
单击图像将切换窗口的位置。
from tkinter import *
def move_me(idx=[0]): # <- creates a closure for the index of the location strings
loc = ["300x300+200+200", "300x300+300+300"]
root.geometry(loc[idx[0]]) # <-- reset the geometry
idx[0] = (idx[0]+1) % 2 # <-- toggles the index between 0 and 1
root = Tk()
root.title("lol")
root.geometry("300x300-100+100")
photo = PhotoImage(file="pepe.gif")
button = Button(root, image=photo, command=move_me)
button.place(x=10, y=10)
root.mainloop()
(attention, you have challenging game to catch the window to close it) (注意,您有挑战性的游戏要赶上窗以将其关闭)
from tkinter import *
import random
def move_me():
x, y = str(random.randrange(800)), str(random.randrange(800))
loc = "300x300+" + x + '+' + y
root.geometry(loc)
root.after(500, move_me) # <-- auto call to move_me again every 1/2 second
root = Tk()
root.title("lol")
root.geometry("300x300+100+100")
photo = PhotoImage(file="pepe.gif")
label = Label(root, image=photo)
label.place(x=10, y=10)
move_me() # <-- call to move_me
root.mainloop()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.