tkinter 标题框架更改 - 与事件绑定和功能作斗争

tkinter title frame change - struggling with event bind and functions

我正在尝试更改 tkinter 标题框的颜色。我正在使用此处找到的结构 Can I change the title bar in Tkinter?

我遇到了一些问题。我一直在修改代码并研究可能的解决方案,但感觉很困难。

Text_input_window= Tk()

def get_pos(event):
    xwin = Text_input_window.winfo_x()
    ywin = Text_input_window.winfo_y()
    startx = event.x_Text_input_window
    starty = event.y_Text_input_window

    ywin = ywin - starty
    xwin = xwin - startx


def move_window(event):
    Text_input_window.geometry("400x400"+'+{0}+{1}'.format(event.x_Text_input_window, event.y_Text_input_window))
    startx = event.x_Text_input_window
    starty = event.y_Text_input_window


Text_input_window.bind('<B1-Motion>', move_window)
Text_input_window.bind('<Button-1>', get_pos)

Text_input_window.overrideredirect(True) #removes default settings in Text_input_window
Text_input_window.geometry('460x250+300+200')

title_bar=Frame(Text_input_window, bg='SteelBlue1', relief='raised', bd=2)

这可能很明显,但我真的不知道这个人做了什么。我已经尝试了很多缩进排列,将函数放在不同的部分 - 函数应该总是在最开始吗? 我认为这是我自己模仿的最好的版本。我得到一个颜色不同的 window 和右上角的 "x",它实际上关闭了。但是 window 没有移动,我得到的错误是:

startx = event.x_Text_input_window
AttributeError: 'Event' object has no attribute 'x_Text_input_window'

ide 还显示 ywin、xwin、startx 和 starty 在 starty = event.y_Text_input_window

行后未被识别或灰色,就像它们没有工作一样

您需要先解决 2 个问题。

  1. 我想你误解了这个属性event.x_root。 x_root 不是因为根 window 在另一个 post 上被命名为根。这只是属性名称,不管您如何称呼您的根 window。所以使用 event.x_root 而不是 event.x_Text_input_window.

  2. 您需要将框架实际放置在 window 上。如果您在该框架中没有任何小部件,则需要定义一个高度,以便您可以在 window.

  3. 上实际看到它

这是您的代码的工作版本:

import tkinter as tk


text_input_window = tk.Tk()


def get_pos(event):
    xwin = text_input_window.winfo_x()
    ywin = text_input_window.winfo_y()
    startx = event.x_root
    starty = event.y_root

    ywin = ywin - starty
    xwin = xwin - startx


def move_window(event):
    text_input_window.geometry("400x400"+'+{0}+{1}'.format(event.x_root, event.y_root))
    startx = event.x_root
    starty = event.y_root


text_input_window.overrideredirect(True)
text_input_window.geometry('460x250+300+200')
text_input_window.columnconfigure(0, weight=1)

title_bar = tk.Frame(text_input_window, height=25, bg='SteelBlue1', relief='raised', bd=5)
title_bar.grid(row=0, column=0, sticky='ew')


text_input_window.bind('<B1-Motion>', move_window)
text_input_window.bind('<Button-1>', get_pos)
text_input_window.mainloop()

结果: