python – 拖放小部件tkinter
发布时间:2020-12-20 12:10:41 所属栏目:Python 来源:网络整理
导读:我正在尝试制作一个 Python程序,您可以在其中移动小部件. 这是我的代码: import tkinter as tk main = tk.Tk()notesFrame = tk.Frame(main,bd = 4,bg = "a6a6a6")notesFrame.place(x=10,y=10)notes = tk.Text(notesFrame)notes.pack()notesFrame.bind("B1-M
我正在尝试制作一个
Python程序,您可以在其中移动小部件.
这是我的代码: import tkinter as tk main = tk.Tk() notesFrame = tk.Frame(main,bd = 4,bg = "a6a6a6") notesFrame.place(x=10,y=10) notes = tk.Text(notesFrame) notes.pack() notesFrame.bind("<B1-Motion>",lambda event: notesFrame.place(x = event.x,y = event.y) 但是,这会出现超级故障并且小部件来回跳跃. 谢谢! 解决方法
您观察到的行为是由事件的坐标相对于拖动的窗口小部件引起的.用相对坐标更新小部件的位置(绝对坐标)显然会导致混乱.
为了解决这个问题,我使用了 class DragDropMixin: def __init__(self,*args,**kwargs): super().__init__(*args,**kwargs) self.drag_start_x = 0 self.drag_start_y = 0 self.bind("<Button-1>",self.drag_start) self.bind("<B1-Motion>",self.drag_motion) def drag_start(self,event): self.drag_start_x = event.x self.drag_start_y = event.y def drag_motion(self,event): x = self.winfo_x() - self.drag_start_x + event.x y = self.winfo_y() - self.drag_start_y + event.y self.place(x=x,y=y) 用法: # As always when it comes to mixins,make sure to # inherit from DragDropMixin FIRST! class DnDFrame(DragDropMixin,tk.Frame): pass # This wouldn't work: # class DnDFrame(tk.Frame,DragDropMixin): # pass main = tk.Tk() notesFrame = DnDFrame(main,bd=4,bg="grey") notesFrame.place(x=10,y=10) notes = tk.Text(notesFrame) notes.pack() (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |