Skip to content

Commit

Permalink
Fixed several window module api issues (#109)
Browse files Browse the repository at this point in the history
  • Loading branch information
israel-dryer authored Jan 2, 2022
1 parent 161bee7 commit 2abfeb4
Showing 1 changed file with 82 additions and 41 deletions.
123 changes: 82 additions & 41 deletions src/ttkbootstrap/window.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,8 +114,9 @@ def __init__(
utility.enable_high_dpi_awareness()

super().__init__()
winsys = self.tk.call('tk', 'windowingsystem')

if scaling:
if scaling is not None:
utility.enable_high_dpi_awareness(self, scaling)

try:
Expand All @@ -127,26 +128,35 @@ def __init__(

self.title(title)

if size:
if size is not None:
width, height = size
self.geometry(f"{width}x{height}")
if position:

if position is not None:
xpos, ypos = position
self.geometry(f"+{xpos}+{ypos}")
if minsize:

if minsize is not None:
width, height = minsize
self.minsize(width, height)
if maxsize:

if maxsize is not None:
width, height
self.maxsize(width, height)
if resizable:

if resizable is not None:
width, height = resizable
self.resizable(width, height)
if transient:

if transient is not None:
self.transient(transient)

if overrideredirect:
self.overrideredirect(1)
if alpha:

if alpha is not None:
if winsys == 'x11':
self.wait_visibility(self)
self.attributes("-alpha", alpha)

self._style = Style(themename)
Expand All @@ -156,11 +166,19 @@ def style(self):
"""Return a reference to the `ttkbootstrap.style.Style` object."""
return self._style

def position_center(self):
"""Position the window in the center of the screen. Does not
account for the titlebar when placing the window.
"""
self.eval("tk::PlaceWindow . center")
def place_window_center(self):
"""Position the toplevel in the center of the screen. Does not
account for titlebar height."""
self.update_idletasks()
w_height = self.winfo_height()
w_width = self.winfo_width()
s_height = self.winfo_screenheight()
s_width = self.winfo_screenwidth()
xpos = (s_width - w_width) // 2
ypos = (s_height - w_height) // 2
self.geometry(f'+{xpos}+{ypos}')

position_center = place_window_center # alias


class Toplevel(tkinter.Toplevel):
Expand All @@ -184,8 +202,7 @@ def __init__(
self,
title="ttkbootstrap",
iconphoto=None,
height=None,
width=None,
size=None,
position=None,
minsize=None,
maxsize=None,
Expand All @@ -208,11 +225,10 @@ def __init__(
The titlebar icon. This image is applied to all future
toplevels as well.
height (int):
Specifies the desired height for the window.
width (int):
Specifies the desired width for the window.
size (Tuple[int, int]):
The width and height of the application window.
Internally, this argument is passed to the
`Toplevel.geometry` method.
position (Tuple[int, int]):
The horizontal and vertical position of the window on
Expand Down Expand Up @@ -273,58 +289,83 @@ def __init__(
Other optional keyword arguments.
"""
super().__init__(**kwargs)
winsys = self.tk.call('tk', 'windowingsystem')

if iconphoto:
self._icon = iconphoto or tkinter.PhotoImage(data=Icon.icon)
self.iconphoto(False, self._icon)

self.title(title)

if position:
if size is not None:
width, height = size
self.geometry(f'{width}x{height}')

if position is not None:
xpos, ypos = position
self.geometry(f"+{xpos}+{ypos}")
if minsize:

if minsize is not None:
width, height = minsize
self.minsize(width, height)
if maxsize:

if maxsize is not None:
width, height
self.maxsize(width, height)
if resizable:

if resizable is not None:
width, height = resizable
self.resizable(width, height)
if transient:

if transient is not None:
self.transient(transient)

if overrideredirect:
self.overrideredirect(1)
if windowtype:
self.attributes("-type", windowtype)

if windowtype is not None:
if winsys == 'x11':
self.attributes("-type", windowtype)

if topmost:
self.attributes("-topmost", 1)

if toolwindow:
self.attributes("-toolwindow", 1)
if alpha:
if winsys == 'win32':
self.attributes("-toolwindow", 1)

if alpha is not None:
if winsys == 'x11':
self.wait_visibility(self)
self.attributes("-alpha", alpha)

@property
def style(self):
"""Return a reference to the `ttkbootstrap.style.Style` object."""
return Style()

def position_center(self):
def place_window_center(self):
"""Position the toplevel in the center of the screen. Does not
account for the titlebar when placing the window.
"""
winfoid = hex(self.winfo_id())
pathname = self.winfo_pathname(winfoid)
self.tk.eval(f"tk::PlaceWindow {pathname} center")

account for titlebar height."""
self.update_idletasks()
w_height = self.winfo_height()
w_width = self.winfo_width()
s_height = self.winfo_screenheight()
s_width = self.winfo_screenwidth()
xpos = (s_width - w_width) // 2
ypos = (s_height - w_height) // 2
self.geometry(f'+{xpos}+{ypos}')

position_center = place_window_center # alias

if __name__ == "__main__":

root = Window(themename="superhero")
root.update_idletasks()
root.position_center()
root = Window(themename="superhero", alpha=0.5, size=(1000, 1000))
#root.withdraw()
root.place_window_center()
#root.deiconify()

top = Toplevel(title="My Toplevel", toolwindow=True, alpha=0.4)
top.position_center()
top = Toplevel(title="My Toplevel", alpha=0.4, size=(1000, 1000))
top.place_window_center()

root.mainloop()

0 comments on commit 2abfeb4

Please sign in to comment.