After removing tkinter default window settings I want to add functionality to move and resize window. Below are the key pieces of code I've taken from a few places and adjusted. I'm trying to make it so when the mouse is below 199 pixels from the top of the window the event response is to resize the window (this is the best I could come up with for two events attached to the mouse). Anyway, I can move the window but after resizing once I can't move again and i get an error:
TypeError: unsupported operand type(s) for +: 'int' and 'str'
on this line:
self.geometry(self.winfo_width() + 'x' + self.winfo_height() + '+{0}+{1}'.format(event.x_root +xwin,event.y_root +ywin))
I've tried to put in numbers to fix the size after its resized. That allows me to move again but the window size is fixed to the figures i replace self.winfo_width() + 'x' + self.winfo_height() +
. so there's got to be something in this line above that I'm overlooking.
I'm ok with a big restructuring/scripting endeavor but if someone would point to my syntax error that would get my make shift script to work that would be great. I'm still trying to build intuition for how classes, events and bindings work. I'm using python 3.7.
from tkinter import *
import tkinter as tk
from tkinter import ttk
# resize class code take from https://stackoverflow.com/questions/22421888/tkinter-windows-without-title-bar-but-resizable
class Example(tk.Tk):
def __init__(self):
tk.Tk.__init__(self)
self.floater = FloatingWindow(self)
self.withdraw()
class FloatingWindow(tk.Toplevel):
def __init__(self, *args, **kwargs):
tk.Toplevel.__init__(self, *args, **kwargs)
# fucntion i brought in to close window
def end_app_2():
self.destroy()
quit()
self.overrideredirect(True)
self.wm_geometry("400x400")
self.label = tk.Label(self, text="Grab the lower-right corner to resize")
self.label.pack(side="bottom", fill="both", expand=True)
self.canvas_2=Canvas(self,bg='steelblue1')
self.canvas_2.pack(anchor='s', side='bottom')
self.title_bar_2 = tk.Frame(self, height=25, bg='SteelBlue1', relief='raised', bd=1)
self.title_bar_2.pack(anchor='n', fill='x', side="bottom" )
self.close_button = Button(self.title_bar_2, text='X', command=end_app_2)
self.close_button.pack( fill='x', side="right" )
self.grip = ttk.Sizegrip(self)
self.grip.place(relx=1.0, rely=1.0, anchor="se")
self.grip.lift(self.label)
self.grip.bind("<B1-Motion>", self.OnMotion)
# move window
def get_pos(event):
xwin = self.winfo_x()
ywin = self.winfo_y()
startx = event.x_root
starty = event.y_root
ywin = ywin - starty
xwin = xwin - startx
def move_window(event):
global size_change
if size_change==True:
self.geometry(self.winfo_width() + 'x' + self.winfo_height() + '+{0}+{1}'.format(event.x_root + xwin, event.y_root + ywin))
else:
self.geometry('400x400' + '+{0}+{1}'.format(event.x_root + xwin, event.y_root + ywin))
if ywin>=-199:
self.bind('<B1-Motion>', move_window)
self.bind('<Button-1>', get_pos)
def OnMotion(self, event):
x1 = self.winfo_pointerx()
y1 = self.winfo_pointery()
x0 = self.winfo_rootx()
y0 = self.winfo_rooty()
self.geometry("%sx%s" % ((x1-x0),(y1-y0)))
global size_change
size_change=True
return
app=Example()
size_change=BooleanVar()
app.mainloop()