2017-03-09 72 views
1

如何修復我的程序的這一部分以將按鈕重新定位到屏幕上的新位置?如何重新定位按鈕?

movingbutton1 = Button(root, text = 'Click Me' ...).place(relx = .5, rely = .5) 
time.sleep(3) 
movingbutton1(root, text = 'Click Me' ...).place(relx = .3, rely = .3) 
+0

請從損壞公物不要你自己的帖子。 – EJoshuaS

回答

2

要移動放置的小部件,您可以使用place_configure(relx=new_x, rely=new_y)

要在用戶單擊按鈕時移動按鈕,請傳遞將此按鈕移動到按鈕的command選項的功能。

import tkinter as tk 
import random 

def move(): 
    x = random.random() 
    y = random.random() 
    moving_button.place_configure(relx=x, rely=y) 

root = tk.Tk() 

moving_button = tk.Button(root, text='Click Me', command=move) 

moving_button.place(relx=0.5, rely=0.5, anchor='center') 

root.mainloop() 
0

可以使用.after方法一定的時間後,移動你的控件,如圖所示:

import tkinter as tk 
import time 

class App(tk.Frame): 
    def __init__(self, parent, *args, **kwargs): 
     tk.Frame.__init__(self, parent, *args, **kwargs) 
     self.button1 = tk.Button(parent, text = 'Click Me') 
     self.button1.place(relx = .5, rely = .5) 
     self.button1.after(3000, self._moveButton) # Call method after 3 secs 

    def _moveButton(self): 
     # reposition button 
     self.button1 .place(relx = .3, rely = .3) 

if __name__ == "__main__": 
    window = tk.Tk() 
    window.geometry('500x200') 
    app = App(window) 
    window.mainloop() 
0

下面的答案是,你要

movingbutton1 = Button(root, text = 'Click Me' ...) 
movingbutton1.place(relx = .5, rely = .5) 
time.sleep(3) 
movingbutton1.place(relx = .3, rely = .3)