0

I am trying to change the color of my tkinter title frame. I'm using the structure found here Can I change the title bar in Tkinter?

I'm having a few problems. I"ve been tinkering with the code and researching possible solutions but feel pretty stuck.

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)

It may be obvious but i don't really know what the person did. I've tried many permutations of indenting placing the functions in different sections - should functions always be at the very beginning? I think this is the best version of what i've mimicked into my own. I get a window with the different color and an "x" on the top right that acutally closes. However the window doesnt' move and the error i'm getting is:

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

The ide is also showing that ywin, xwin, startx and starty aren't recognized or gray like they arent' working after the line starty = event.y_Text_input_window

costa rica
  • 85
  • 1
  • 12
  • Please provide a complete testable example. You are missing at least the imports and mainloop. That said you never add title_bar to your window. You are missing `grid()` or `pack()`. – Mike - SMT Jan 24 '20 at 21:57
  • I think you misunderstood this attribute `event.x_root`. the x_root was not because the root window was named root on the other post. That is just the attribute name regardless of what you call your root window. So use `event.x_root` instead of `event.x_Text_input_window`. – Mike - SMT Jan 24 '20 at 22:06

1 Answers1

1

You need to fix 2 problems first.

  1. I think you misunderstood this attribute event.x_root. the x_root was not because the root window was named root on the other post. That is just the attribute name regardless of what you call your root window. So use event.x_root instead of event.x_Text_input_window.

  2. You need to actually place the Frame on the window. And if you do not have any widgets in that frame you will need to define a height so you can actually see it on the window.

Here is a working version of your code:

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

Results:

enter image description here

Mike - SMT
  • 14,784
  • 4
  • 35
  • 79
  • That definitely helps. yes I misunderstood 'root'. I having trouble moving the window. when i move it the widgets get disoriented and my cursor goes to the top left of the window. – costa rica Jan 25 '20 at 16:07