Python 自定义标题栏和关闭按钮Tkinter

Python 自定义标题栏和关闭按钮Tkinter,python,python-3.x,tkinter,titlebar,Python,Python 3.x,Tkinter,Titlebar,我在stackoverflow上找到了以下代码: from tkinter import * root=Tk() root.overrideredirect(True) # turns off title bar, geometry root.geometry('400x100+200+200') # set new geometry # make a frame for the title bar title_bar = Frame(root, bg='#2e2e2e', relief='r

我在stackoverflow上找到了以下代码:

from tkinter import *
root=Tk()
root.overrideredirect(True) # turns off title bar, geometry
root.geometry('400x100+200+200') # set new geometry

# make a frame for the title bar
title_bar = Frame(root, bg='#2e2e2e', relief='raised', bd=2,highlightthickness=0)

# put a close button on the title bar
close_button = Button(title_bar, text='X', command=root.destroy,bg="#2e2e2e",padx=2,pady=2,activebackground='red',bd=0,font="bold",fg='white',highlightthickness=0)

# a canvas for the main area of the window
window = Canvas(root, bg='#2e2e2e',highlightthickness=0)

# pack the widgets
title_bar.pack(expand=1, fill=X)
close_button.pack(side=RIGHT)
window.pack(expand=1, fill=BOTH)
xwin=None
ywin=None
# bind title bar motion to the move window function

def move_window(event):
    root.geometry('+{0}+{1}'.format(event.x_root, event.y_root))
def change_on_hovering(event):
    global close_button
    close_button['bg']='red'
def return_to_normalstate(event):
    global close_button
    close_button['bg']='#2e2e2e'


title_bar.bind('<B1-Motion>', move_window)
close_button.bind('<Enter>',change_on_hovering)
close_button.bind('<Leave>',return_to_normalstate)
root.mainloop()
从tkinter导入*
root=Tk()
root.overrideredirect(True)#关闭标题栏、几何图形
根.几何体('400x100+200+200')#设置新几何体
#为标题栏创建一个框架
标题栏=框架(根,背景='#2E',浮雕='凸起',bd=2,高亮厚度=0)
#在标题栏上放置“关闭”按钮
关闭按钮=按钮(标题栏,text='X',command=root.destroy,bg=“#2e”,padx=2,pady=2,activebackground='red',bd=0,font=“bold”,fg='white',highlightthickness=0)
#窗口主要区域的画布
window=Canvas(根,bg='#2e',highlightthickness=0)
#打包小部件
标题栏包装(展开=1,填充=X)
关闭按钮。包装(侧面=右侧)
window.pack(扩展=1,填充=2)
xwin=无
ywin=无
#将标题栏运动绑定到“移动窗口”功能
def move_窗口(事件):
几何体('+{0}+{1}'。格式(event.x_root,event.y_root))
def change_on_悬停(事件):
全局关闭按钮
关闭按钮['bg']=“红色”
def返回正常状态(事件):
全局关闭按钮
关闭按钮['bg']='2E'
标题栏绑定(“”,移动窗口)
关闭按钮。绑定(“”,在悬停时更改)
关闭按钮。绑定(“”,返回正常状态)
root.mainloop()

问题是窗口的移动非常奇怪,有没有办法通过更改代码来修复它?

修复:

from tkinter import *
from webbrowser import *

root=Tk()
root.overrideredirect(True) # turns off title bar, geometry
root.geometry('400x100+200+200') # set new geometry
root.attributes('-topmost', True)

lastClickX = 0
lastClickY = 0

def SaveLastClickPos(event):
    global lastClickX, lastClickY
    lastClickX = event.x
    lastClickY = event.y

def Dragging(event):
    x, y = event.x - lastClickX + root.winfo_x(), event.y - lastClickY + root.winfo_y()
    root.geometry("+%s+%s" % (x , y))

# make a frame for the title bar
title_bar = Frame(root, bg='#2e2e2e', relief='raised', bd=2,highlightthickness=0)

# put a close button on the title bar
close_button = Button(title_bar, text='X', command=root.destroy,bg="#2e2e2e",padx=2,pady=2,activebackground='red',bd=0,font="bold",fg='white',highlightthickness=0)

window = Canvas(root, bg='#2e2e2e',highlightthickness=0)

# pack the widgets
title_bar.pack(expand=1, fill=X)
close_button.pack(side=RIGHT)
window.pack(expand=1, fill=BOTH)

title_bar.bind('<Button-1>', SaveLastClickPos)
title_bar.bind('<B1-Motion>', Dragging)
root.mainloop()
从tkinter导入*
从webbrowser导入*
root=Tk()
root.overrideredirect(True)#关闭标题栏、几何图形
根.几何体('400x100+200+200')#设置新几何体
属性('-topmest',True)
lastClickX=0
lastClickY=0
def SaveLastClickPos(事件):
全局lastClickX,lastClickY
lastClickX=event.x
lastClickY=event.y
def拖动(事件):
x、 y=event.x-lastClickX+root.winfo_x(),event.y-lastClickY+root.winfo_y()
根几何体(“++%s++%s”%(x,y))
#为标题栏创建一个框架
标题栏=框架(根,背景='#2E',浮雕='凸起',bd=2,高亮厚度=0)
#在标题栏上放置“关闭”按钮
关闭按钮=按钮(标题栏,text='X',command=root.destroy,bg=“#2e”,padx=2,pady=2,activebackground='red',bd=0,font=“bold”,fg='white',highlightthickness=0)
window=Canvas(根,bg='#2e',highlightthickness=0)
#打包小部件
标题栏包装(展开=1,填充=X)
关闭按钮。包装(侧面=右侧)
window.pack(扩展=1,填充=2)
标题栏绑定(“”,SaveLastClickPos)
标题栏绑定(“”,拖动)
root.mainloop()

检查此项:非常好,谢谢!