[英]python tkinter , move frame to his old postion
我希望框架在向右移動后回來。 我怎樣才能做到這一點? 還是框架從右側進入左側,以產生滑動效果?
def move(steps=10, distance=0.10, distance2=0.10):
if steps > 10:
# get current position
relx = float(frame.place_info()['relx'])
# set new position
frame.place_configure(relx=relx+distance)
# repeate it after 10ms
window.after(10, move, steps-1, distance)
def on_click():
# start move
move(50, 0.02) # 50*0.02 = 1
frame = Frame(window, bg="red", width=400, height=400)
frame.place(relx=0.192, rely=0.001, relwidth=0.81, relheight=0.992)
我嘗試了幾個選項,但我找不到辦法讓框架在移動后恢復到舊位置。 有人有什么想法嗎?
像下面這樣的東西會起作用。 這個概念很簡單: place
的relx
或rely
不斷變化,直到Frame
達到它的最終“rel-position”。 我們使用一點比率數學來確保框架被完全推出顯示器並帶回所需的“相對位置”。 我很無聊,所以我讓它在各個方向都起作用。
import tkinter as tk
class SlideFrame(tk.Frame):
def __init__(self, master, **kwargs):
tk.Frame.__init__(self, master, **kwargs)
self.__slide = None
def slide_in(self, ult_rel=0, steps=20, side='left', inc=None):
#force everything to be perfect no matter what
if inc is None:
self.update_idletasks()
#determine the ratio of self to master in the proper direction
if side in ('left', 'right'):
r = self.winfo_width()/self.master.winfo_width()
elif side in ('top', 'bottom'):
r = self.winfo_height()/self.master.winfo_height()
#determine the extra amount to move beyond ratio
ex = ult_rel if side in ('left', 'top') else r-ult_rel
#divide the sum by steps to get a move increment
inc = (r+ex)/steps
#get the proper polarity to multiply by
s = -steps if side in ('left', 'top') else steps
#get new position
p = ult_rel+(s*inc)
#determine which property to apply the move to
self.place_configure(relx=p) if side in ('left', 'right') else self.place_configure(rely=p)
#as long as there are steps left, keep running this method
if steps > 0:
self.slide = self.after(20, self.slide_in, ult_rel, steps-1, side, inc)
def place_(self, ult_rel=0, side='left', steps=20, **kwargs):
#use this little trick and you can do everything on one line
self.place(**kwargs)
self.slide_in(ult_rel, steps, side)
return self
root = tk.Tk()
root.geometry('800x600+100+100')
root.configure(background='#111111')
root.update_idletasks()
dims=dict(relwidth=.5, relheight=.5)
#demo
frame1 = SlideFrame(root, bg='#ff0000').place_( 0, rely= 0, **dims)
frame2 = SlideFrame(root, bg='#00ff00').place_( 0, 'top' , relx=.5, **dims)
frame3 = SlideFrame(root, bg='#0000ff').place_(.5, 'right' , rely=.5, **dims)
frame4 = SlideFrame(root, bg='#ff8800').place_(.5, 'bottom', relx= 0, **dims)
if __name__ == '__main__':
root.mainloop()
嘗試這個:
import tkinter as tk
def move_frame(event=None):
# 1 is the change in x and the 0 is the change in y
canvas.move(frame_id, 1, 0)
# Look at: https://stackoverflow.com/a/2679823/11106801
x, *_ = canvas.coords(frame_id)
# Note 400 is the width of the canvas
if x > 400:
canvas.move(frame_id, -400, 0)
else:
# To adjust the speed change the 3 (it's the time in ms)
# It calls `move_frame` after 3 ms
canvas.after(3, move_frame)
root = tk.Tk()
# Create the canvas
canvas = tk.Canvas(root, width=400, height=400, bg="black")
canvas.pack()
frame = tk.Frame(canvas, width=100, height=100, bg="grey")
# Look at: https://stackoverflow.com/a/11981214/11106801
frame_id = canvas.create_window(0, 150, anchor="nw", window=frame)
# If you want it to move when clicked:
frame.bind("<Button-1>", move_frame)
# To just start the movement:
canvas.after(0, move_frame)
# Make sure you start the mainloop otherwise the loop woudn't work
root.mainloop()
它使用 tkinter 循環在 canvas 上移動 object。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.