简体   繁体   English

创建python后如何使用python移动tkinter窗口?

[英]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()

[edit] with auto repeat moving the window. [编辑]自动重复移动窗口。

(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.

相关问题 创建后更新python生成器 - updating a python generator after it has been created Tkinter-我如何在新窗口中创建按钮,该窗口是由调用函数创建的? Python 3 - Tkinter - How would I create buttons in a new window, which has been created by a function being called? Python 3 如何检查之前是否打开过 tkinter 应用程序? (Tkinter python) - How to check if a tkinter app has been opened before? (Tkinter python) 如何在 Tkinter 中完成流程后再次启用元素 - How to enable an element again after a process has been completed in Tkinter 在 Python 中创建 DataFrame 后应用转换器字典中的转换 - Applying the conversions in the converters dictionary after a DataFrame has been created in Python 如何在创建matplotlib图例后对其进行修改? - How to modify matplotlib legend after it has been created? 创建 QApplication 后如何导入 QtWebEngineWidgets - How to import QtWebEngineWidgets after QApplication has been created Anaconda平台升级后,如何从Windows cmd启动python控制台? - How can I start python console from Windows cmd after Anaconda Platform has been upgraded? 如何填充已经在python中创建的空字符串 - How to fill an empty string which has already been created in python python Tkinter,检查root是否已被破坏? - python Tkinter, check if root has been destroyed?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM