简体   繁体   English

拖动时按钮强制鼠标进入窗口的左上角

[英]Buttons force mouse into top left of window when dragging

I've set up a basic code that makes a draggable overrideredirect tkinter window, but when I add my own custom buttons the window doesnt drag with the buttons but instead flicks the mouse to the top left corner and drags the window from there. 我已经设置了一个基本代码,它创建了一个可拖动的overrideredirect tkinter窗口,但是当我添加自己的自定义按钮时,窗口不会拖动按钮,而是将鼠标滑动到左上角并从那里拖动窗口。

#Imports
import sqlite3,tkinter
#Connects to the database
with sqlite3.connect("apc.db") as db:
    cursor = db.cursor()

#Interface Class
class login_UI(tkinter.Tk):
    #Main Interface Function
    def __init__(self, master=None):
        tkinter.Tk.__init__(self, master)
        self.title("Apex Companion")
        self.geometry()
        self.geometry("250x400")
        self.overrideredirect(True)
        self.config(bg="#1e1e1e")
        #Tob Bar Frame
        tb = tkinter.Frame(self,height=20,width=250,bg="#0f0f0f")
        tb.pack_propagate(False)
        #Top Bar Text
        tb_text = tkinter.Label(tb, text="Apex Companion",bg="#0f0f0f",fg="#b4b4b4")
        tb_text.config(font=("Trebuchet",10, "bold"))
        #Top Bar Close
        tb_close = tkinter.Button(tb, height=2, width=3,
                                  text="✕", bg="#0f0f0f", fg="#ffffff",
                                  activebackground="#c94343",activeforeground="#ffffff",
                                  command=self.destroy, bd=0)
        #Top Bar Minimize
        tb_min = tkinter.Button(tb, height=2, width=2,
                                text="—", bg="#0f0f0f",fg="#ffffff"
                                ,bd=0,activeforeground="#ff4e1d",activebackground="#0f0f0f")
        #Top Bar Logo
        self.tb_img = tkinter.PhotoImage(file="logo_apc.gif")
        tb_logo = tkinter.Label(tb,image=self.tb_img,bd=0,justify=tkinter.RIGHT)


        #Top Bar Packing
        tb.pack()
        tb_close.pack(side=tkinter.RIGHT)
        tb_min.pack(side=tkinter.RIGHT)
        tb_logo.pack(side=tkinter.LEFT)
        tb_text.pack()

        #Make Window Draggable
        self._offsetx = 200
        self._offsety = 200
        self.bind('<Button-1>', self.clickwin)
        self.bind('<B1-Motion>', self.dragwin)
    #Window Dragging Events
    def dragwin(self,event):
        x = self.winfo_pointerx() - self._offsetx
        y = self.winfo_pointery() - self._offsety
        self.geometry('+{x}+{y}'.format(x=x,y=y))

    def clickwin(self,event):
        self._offsetx = event.x_root
        self._offsety = event.y_root

    def login_db(self,event):
        print("E")
#Initilize the Interface
login_UI = login_UI()
login_UI.mainloop()

I've updated the code to display my entire file. 我已经更新了代码以显示我的整个文件。 Hopefully this should help. 希望这应该有所帮助。

when you click on button then event.x , event.y gives position relative to top-left corner of Button , not to top-left corner of window so later it gives wrong results. 当你点击按钮然后event.xevent.y给出相对于Button左上角的位置,而不是窗口的左上角,所以稍后它会给出错误的结果。

this gives correct offset 这给出了正确的偏移

def clickwin(self, event):
    self._offsetx = self.winfo_pointerx() - self.winfo_rootx()
    self._offsety = self.winfo_pointery() - self.winfo_rooty()

Now I can move even by dragging Button but dragging button later gives other problem - when I stop draging then button catch click and it close window :) 现在我甚至可以通过拖动按钮移动,但拖动按钮稍后会出现其他问题 - 当我停止拖动然后按钮捕获点击它关闭窗口:)


Full code 完整代码

import tkinter

class Win(tkinter.Tk):

    def __init__(self, master=None):
        tkinter.Tk.__init__(self, master)

        #self.geometry()
        self.geometry("250x400")
        self.overrideredirect(True)
        self.config(bg="#1e1e1e")

        #Tob Bar Frame
        tb = tkinter.Frame(self, height=20, width=250, bg="#0f0f0f")
        tb.pack_propagate(False)
        tb.pack()

        #Top Bar Close
        tb_close = tkinter.Button(tb, height=2, width=3,
                                  text="✕", bg="#0f0f0f", fg="#ffffff",
                                  activebackground="#c94343",activeforeground="#ffffff",
                                  command=self.destroy, bd=0)
        tb_close.pack()

        #Make Window Draggable
        self._offsetx = 0
        self._offsety = 0
        self.bind('<B1-Motion>', self.dragwin)
        self.bind('<Button-1>', self.clickwin)

    #Window Dragging Events
    def dragwin(self,event):
        x = self.winfo_pointerx() - self._offsetx
        y = self.winfo_pointery() - self._offsety
        self.geometry('+{x}+{y}'.format(x=x,y=y))

    def clickwin(self, event):
        self._offsetx = self.winfo_pointerx() - self.winfo_rootx()
        self._offsety = self.winfo_pointery() - self.winfo_rooty()
        print('1.', event.x, event.y)
        print('2.', event.x_root, event.y_root)
        print('3.', self._offsetx, self._offsety)

root = Win()
root.mainloop()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM