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

我在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()

http://img4.mukewang.com/63578d9b0001e3d004050104.jpg

问题是窗口的移动很奇怪,有没有办法通过更改代码来解决它?



元芳怎么了
浏览 218回答 1
1回答

斯蒂芬大帝

使固定:from tkinter import *from webbrowser import *root=Tk()root.overrideredirect(True) # turns off title bar, geometryroot.geometry('400x100+200+200') # set new geometryroot.attributes('-topmost', True)lastClickX = 0lastClickY = 0def SaveLastClickPos(event):&nbsp; &nbsp; global lastClickX, lastClickY&nbsp; &nbsp; lastClickX = event.x&nbsp; &nbsp; lastClickY = event.ydef Dragging(event):&nbsp; &nbsp; x, y = event.x - lastClickX + root.winfo_x(), event.y - lastClickY + root.winfo_y()&nbsp; &nbsp; root.geometry("+%s+%s" % (x , y))# make a frame for the title bartitle_bar = Frame(root, bg='#2e2e2e', relief='raised', bd=2,highlightthickness=0)# put a close button on the title barclose_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 widgetstitle_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()
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python