我已经建立了一个基本代码,该代码使可拖动的overrideredirecttkinter窗口成为可能,但是当我添加自己的自定义按钮时,该窗口不随按钮一起拖动,而是将鼠标轻拂到左上角并从那里拖动窗口
#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()
我已经更新了代码以显示我的整个文件。希望这会有所帮助。
答案 0 :(得分:0)
当您单击按钮然后event.x
时,event.y
给出相对于Button
的左上角的位置,而不是相对于窗口的左上角的位置,因此以后给出错误的结果。
这给出了正确的偏移量
def clickwin(self, event):
self._offsetx = self.winfo_pointerx() - self.winfo_rootx()
self._offsety = self.winfo_pointery() - self.winfo_rooty()
现在我什至可以通过拖动Button来移动,但是稍后拖动Button会带来其他问题-当我停止拖动时,按钮会单击并关闭窗口:)
完整代码
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()