I have already created functions to handle that. But it's shacking when i try to move( After add more widgets). Can somebody help me with that.
from tkinter import *
from PIL import Image, ImageTk
import time
class modtk(Tk):
toptitlebar = None
maincanvaswindow = None
resizeon = False
dragging = False
def updatetopbut(self, event):
self.closebutton.config(fg=self.ttfg)
self.fullscreenbutton.config(fg=self.ttfg)
self.minimizebutton.config(fg=self.ttfg)
def downdatetopbut(self, event):
self.closebutton.config(fg=self.ttbg)
self.fullscreenbutton.config(fg=self.ttbg)
self.minimizebutton.config(fg=self.ttbg)
def modify(self, title="tk", icon=None, titlebg="#ffffff", titlefg="#000000", bg="#EBEBEB"):
self.tt = title
self.ico = icon
self.ttbg = titlebg
self.ttfg = titlefg
self.bgbg = bg
self.title(title)
self.width = 150
self.height = 200
self.maximized = False
self.map = 0
self.resizeside = None
self.moveok = False
self.minsizex = 0
self.minsizey = 0
self.resizeadjx = self.winfo_rootx()
self.resizeadjy = self.winfo_rooty()
self.update_idletasks()
self.overrideredirect(True)
self.config(bg=self.bgbg)
if not icon == None:
try:
icon = Image.open(icon)
icon = icon.resize((30, 30))
icon = ImageTk.PhotoImage(icon)
except Exception as e:
print(e, "File should be a .png file.")
icon = None
__class__.toptitlebar = Frame(self, height=34, bd=0, highlightthickness=1, highlightbackground="#000000",
bg=titlebg)
__class__.toptitlebar.pack(side=TOP, fill=X)
__class__.toptitlebar.bind("<ButtonPress-1>", self.SaveLastClickPos)
__class__.toptitlebar.bind("<B1-Motion>", self.Dragging)
__class__.toptitlebar.bind("<Double-Button-1>", self.maximize)
__class__.toptitlebar.bind("<Enter>", self.updatetopbut)
__class__.toptitlebar.bind("<Leave>", self.downdatetopbut)
__class__.toptitlebar.bind("<ButtonRelease-1>", self.releaseresize)
self.titlelabel = Label(__class__.toptitlebar, bg=titlebg, fg=titlefg, bd=0, padx=0, pady=0)
self.titlelabel.image = icon
self.titlelabel.config(image=icon, compound=LEFT, text=" %s" % title)
self.titlelabel.place(x=0, y=0)
self.titlelabel.bind("<ButtonPress-1>", self.SaveLastClickPos)
self.titlelabel.bind("<B1-Motion>", self.Dragging)
self.titlelabel.bind("<Double-Button-1>", self.maximize)
self.closefunction = lambda event: self.destroy()
self.closebutton = Label(__class__.toptitlebar, text="X",
bd=0,
bg=titlebg, fg=titlebg,
padx=14, pady=7)
self.closebutton.pack(side=RIGHT)
self.closebutton.bind("<Enter>", lambda event: self.closebutton.config(bg="#FF0000"))
self.closebutton.bind("<Leave>", lambda event: self.closebutton.config(bg=titlebg))
self.closebutton.bind("<Button-1>", self.closefunction)
self.fullscreenbutton = Label(__class__.toptitlebar, text="[]",
bd=0,
bg=titlebg, fg=titlebg,
padx=14, pady=7)
self.fullscreenbutton.pack(side=RIGHT)
self.fullscreenbutton.bind("<Enter>", lambda event: self.fullscreenbutton.config(bg="#dddddd"))
self.fullscreenbutton.bind("<Leave>", lambda event: self.fullscreenbutton.config(bg=titlebg))
self.fullscreenbutton.bind("<Button-1>", self.maximize)
self.minimizebutton = Label(__class__.toptitlebar, text="-",
bd=0,
bg=titlebg, fg=titlebg,
padx=14, pady=7)
self.minimizebutton.pack(side=RIGHT)
self.minimizebutton.bind("<Enter>", lambda event: self.minimizebutton.config(bg="#dddddd"))
self.minimizebutton.bind("<Leave>", lambda event: self.minimizebutton.config(bg=titlebg))
self.minimizebutton.bind("<Button-1>", self.minimize)
self.bind("<Map>", self.frame_mapped)
__class__.maincanvaswindow = Canvas(self, width=self.width, height=self.height, bg=bg, highlightthickness=1,
highlightbackground="#000000")
__class__.maincanvaswindow.pack(side=TOP)
self.bind("<Button-1>", self.startresize)
self.bind("<B1-Motion>", self.resizewin)
self.bind("<Motion>", self.isonresize)
self.bind("<ButtonRelease-1>", self.releaseresize)
self.lastClickX = 0
self.lastClickY = 0
self.map = 0
self.maximized = False
`Inline Code Example Here`
def modgeometry(self, w, h, x=None, y=None):
w = int(w)
h = int(h)
self.width = w
self.height = h
if (__class__.maincanvaswindow.winfo_width() != w or __class__.maincanvaswindow.winfo_height() != h) and not __class__.resizeon and not __class__.dragging:
__class__.maincanvaswindow.config(width=w, height=h)
if not x == None or not y == None:
x = int(x)
y = int(y)
self.geometry('%dx%d+%d+%d' % (w, (h + 33), x, y))
else:
self.geometry('%dx%d' % (w, (h + 33)))
`Inline Code Example Here`
def SaveLastClickPos(self, event):
if not self.maximized:
if event.y > 5 and event.x > 5 and event.x < (self.winfo_width() - 5):
self.lastClickX = event.x
self.lastClickY = event.y
self.moveok = True
else:
self.moveok = False
`Inline Code Example Here`
def Dragging(self, event):
if self.moveok:
if not self.maximized:
x = self.winfo_x() + (event.x - self.lastClickX)
y = self.winfo_y() + (event.y - self.lastClickY)
self.modgeometry(self.winfo_width(), (self.winfo_height() - 33), x, y)
def frame_mapped(self, event=None):
print(event)
if self.map == 5:
print("yes")
self.update_idletasks()
# self.deiconify()
self.overrideredirect(True)
self.map = 0
else:
self.map += 1
def minimize(self, event=None):
print("no")
#self.map = 0
self.update_idletasks()
self.overrideredirect(False)
self.iconify()
def maximize(self, event=None):
if not self.maximized:
self.oldwidth = self.winfo_width()
self.oldheight = self.winfo_height()
self.oldx = self.winfo_x()
self.oldy = self.winfo_y()
self.maximized = True
self.modgeometry(self.winfo_screenwidth(), (self.winfo_screenheight() - 33), 0, 0)
else:
self.maximized = False
self.modgeometry(self.oldwidth, self.oldheight, self.oldx, self.oldy)
def closefunc(self, func):
self.closefunction = lambda event: func()
self.closebutton.bind("<Button-1>", self.closefunction)
def winfo_heightr(self):
return (self.winfo_height() - 33)
def startresize(self, event=None):
print(event)
if not self.maximized:
#if event.widget == __class__.maincanvaswindow:
if True:
self.resizelastx = event.x
self.resizelasty = event.y
self.resizelastw = self.winfo_width()
self.resizelasth = self.winfo_height() - 33
self.resizeadjx = self.winfo_rootx()
self.resizeadjy = self.winfo_rooty()
__class__.resizeon = True
print("canvas")
nonety = True
## if event.x < 5 and event.y < (self.winfo_height() - 33 -5):
## self.resizeside = "left"
## nonety = False
if event.x > (self.winfo_width() - 5) and event.y < (self.winfo_height() - 33 -5):
self.resizeside = "right"
nonety = False
if event.y >(self.winfo_height() -33 - 5) and event.x > 5 and event.x < (self.winfo_width() - 5):
self.resizeside = "down"
nonety = False
if event.x > (self.winfo_width() - 5) and event.y > (self.winfo_height() - 33 - 5):
self.resizeside = "rightdown"
nonety = False
## if event.x < 5 and event.y > (self.winfo_height() -33 - 5):
## self.resizeside = "leftdown"
## nonety = False
if nonety:
self.resizeside = ""
print(self.resizeside)
## elif event.widget == __class__.toptitlebar:
## self.resizelastx = event.x
## self.resizelasty = event.y
## self.resizelastw = self.winfo_width()
## self.resizelasth = self.winfo_height() - 33
## self.resizeadjx = self.winfo_rootx()
## self.resizeadjy = self.winfo_rooty()
## print("titlebar")
## nonety = True
## if event.x < 5 and event.y > 5:
## self.resizeside = "left"
## nonety = False
## if event.y < 5 and event.x > 5 and event.x < (self.winfo_width() - 5):
## self.resizeside = "up"
## nonety = False
## if event.x < 5 and event.y < 5:
## self.resizeside = "leftup"
## nonety = False
## if event.x > (self.winfo_width() - 5) and event.y > 5:
## self.resizeside = "right"
## nonety = False
## if event.x > (self.winfo_width() - 5) and event.y < 5:
## self.resizeside = "rightup"
## nonety = False
## if nonety:
## self.resizeside = ""
## else:
## self.resizeside = ""
def isonresize(self, event=None):
print(event)
if not self.maximized:
#if event.widget == __class__.maincanvaswindow:
if True:
print("canvas")
nonety = True
## if event.x < 5:
## self.config(cursor="size_we")
## nonety = False
if event.x > (self.winfo_width() - 5):
self.config(cursor="size_we")
nonety = False
if event.y >(self.winfo_height() - 33 - 5):
self.config(cursor="size_ns")
nonety = False
if event.x > (self.winfo_width() - 5) and event.y > (self.winfo_height() - 33 - 5):
self.config(cursor="size_nw_se")
nonety = False
## if event.x < 5 and event.y > (self.winfo_height() - 33 - 5):
## self.config(cursor="size_ne_sw")
## nonety = False
if nonety:
self.config(cursor="")
## elif event.widget == __class__.toptitlebar:
## print("titlebar")
## nonety = True
## if event.x < 5 and event.y > 5:
## self.config(cursor="size_we")
## nonety = False
## if event.y < 5 and event.x > 5 and event.x < (self.winfo_width() - 5):
## self.config(cursor="size_ns")
## nonety = False
## if event.x < 5 and event.y < 5:
## self.config(cursor="size_nw_se")
## nonety = False
## if event.x > (self.winfo_width() - 5) and event.y > 5:
## self.config(cursor="size_we")
## nonety = False
## if event.x > (self.winfo_width() - 5) and event.y < 5:
## self.config(cursor="size_ne_sw")
## nonety = False
## if nonety:
## self.config(cursor="")
## else:
## self.config(cursor="")
def resizewin(self, event=None):
print(event)
if not self.maximized:
if self.resizeside == "right":
neww = self.width + (event.x - self.resizelastx)
print(self.resizeside)
if neww < self.minsizex:
neww = self.minsizex
self.resizelastx = neww
self.modgeometry(neww, self.resizelasth)
if self.resizeside == "rightdown":
print(self.resizeside)
neww = self.width + (event.x - self.resizelastx)
newh = self.height + (event.y - self.resizelasty)
if neww < self.minsizex:
neww = self.minsizex
if newh < self.minsizey:
newh = self.minsizey
self.resizelastx = neww
self.resizelasty = newh
self.modgeometry(neww, newh)
if self.resizeside == "down":
newh = self.height + (event.y - self.resizelasty)
if newh < self.minsizey:
newh = self.minsizey
self.resizelasty = newh
self.modgeometry(self.resizelastw, newh)
if self.resizeside == "rightup":
print(self.resizeside)
neww = self.width + (event.x - self.resizelastx)
newh = self.resizelasth + (self.resizelasty - event.y)
if neww < self.minsizex:
neww = self.minsizex
if newh < self.minsizey:
newh = self.minsizey
self.resizelastx = neww
newy = self.resizeadjy - (self.resizelasty - event.y)
newx = self.resizeadjx
self.modgeometry(neww, newh, newx, newy)
if self.resizeside == "up":
print(self.resizeside)
newh = self.resizelasth + (self.resizelasty - event.y)
if newh < self.minsizey:
newh = self.minsizey
newy = self.resizeadjy - (self.resizelasty - event.y)
newx = self.resizeadjx
self.modgeometry(self.resizelastw, newh, newx, newy)
if self.resizeside == "leftup":
print(self.resizeside)
neww = self.resizelastw + (self.resizelastx - event.x)
newh = self.resizelasth + (self.resizelasty - event.y)
if neww < self.minsizex:
neww = self.minsizex
if newh < self.minsizey:
newh = self.minsizey
newy = self.resizeadjy - (self.resizelasty - event.y)
newx = self.resizeadjx - (self.resizelastx - event.x)
self.modgeometry(neww, newh, newx, newy)
if self.resizeside == "left":
print(self.resizeside)
neww = self.resizelastw + (self.resizelastx - event.x)
newx = self.resizeadjx - (self.resizelastx - event.x)
if neww < self.minsizex:
neww = self.minsizex
newx = self.resizeadjx
newy = self.resizeadjy
self.modgeometry(neww, self.resizelasth, newx, newy)
if self.resizeside == "leftdown":
print(self.resizeside)
neww = self.resizelastw + (self.resizelastx - event.x)
newh = self.height + (event.y - self.resizelasty)
if neww < self.minsizex:
neww = self.minsizex
if newh < self.minsizey:
newh = self.minsizey
self.resizelasty = newh
newx = self.resizeadjx - (self.resizelastx - event.x)
newy = self.resizeadjy
self.modgeometry(neww, self.resizelasth, newx, newy)
def releaseresize(self, event=None):
if __class__.resizeon:
__class__.resizeon = False
self.width = self.winfo_width()
self.height = self.winfo_heightr()
__class__.maincanvaswindow.config(width=self.winfo_width(), height=self.winfo_heightr())
if __class__.dragging:
__class__.dragging = False
def modminsize(self, event=None, w=None, h=None):
if w != None:
self.minsizex = w
if h != None:
self.minsizey = h
def updatewin(self):
self.update()
def wmroot(self):
return __class__.maincanvaswindow
def retwm(self):
return self
if __name__ == "__main__":
root = modtk()
root.modify(title="Modified window", titlebg="#000427", titlefg="#ffffff")
root.modgeometry(600, 400)
root.modminsize(600, 400)
label01 = Label(root.wmroot(), text="This works")
def closewin():
root.destroy()
quit()
root.closefunc(closewin)
windowExit = False
while not windowExit:
label01.place(x=(root.winfo_width() // 2), y=(root.winfo_heightr() // 2))
root.updatewin()
time.sleep(0.05)
closewin()