Skip to content

Commit 628908b

Browse files
committed
Dynamic canvas size in relation to the app size
1 parent 912cded commit 628908b

File tree

1 file changed

+25
-3
lines changed

1 file changed

+25
-3
lines changed

src/gui.py

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@
88

99
import sv_ttk
1010

11+
MIN_WIDTH = 1165
12+
MIN_HEIGHT = 630
13+
1114
class GUI(tk.Tk):
1215
''''''
1316
BACKGROUND_COLOR = '#131313'
@@ -19,7 +22,7 @@ class GUI(tk.Tk):
1922
POINT_SIZE = 1
2023
POINT_COLOR = '#131313'
2124

22-
def __init__(self, title = '3D_Viz', min_size = (1165, 630)):
25+
def __init__(self, title='3D_Viz', min_size=(MIN_WIDTH, MIN_HEIGHT)):
2326
''''''
2427
super().__init__()
2528
# Set the theme to be dark (there must be an initialized app)
@@ -29,6 +32,9 @@ def __init__(self, title = '3D_Viz', min_size = (1165, 630)):
2932
self._changed = True # A flag used to only redraw the object when a change occured
3033
self._geometry_handler = Geometry(self.CANVAS_WIDTH, self.CANVAS_HEIGHT)
3134

35+
self._canvas_w = int((self.CANVAS_WIDTH/MIN_WIDTH)*MIN_WIDTH)
36+
self._canvas_h = int((self.CANVAS_HEIGHT/MIN_HEIGHT)*MIN_HEIGHT)
37+
3238
self._fill_color_holder = "#000000"
3339
self._line_color_holder = "#0000FF"
3440

@@ -65,8 +71,9 @@ def __create_widgets(self):
6571
def __create_canvas(self):
6672
self._canvas_color = tk.StringVar()
6773
self._canvas_color.set("#FFFFFF")
68-
self._canvas = tk.Canvas(self, width=self.CANVAS_WIDTH, height=self.CANVAS_HEIGHT, bg=self._canvas_color.get())
69-
self._canvas.place(relx=0.03, rely=0.052)
74+
self._canvas = tk.Canvas(self, bg=self._canvas_color.get())
75+
self._canvas.place(relx=0.03, rely=0.052, relwidth=0.72, relheight=0.83)
76+
self._canvas.bind("<Configure>", self.__resized)
7077

7178
def __create_zoom_slider(self):
7279
ttk.Label(self, text="Zoom:", foreground="#ffffff", background="#131113").place(relx=self.COMMON_X, rely=0.052, relheight=0.035, relwidth=0.2, anchor="ne")
@@ -174,6 +181,21 @@ def __pick_color(self, picker):
174181

175182
self.__changed()
176183

184+
def __get_canvas_shape(self):
185+
"""returns the shape of the canvas holding the visualized frame"""
186+
self.update()
187+
return self._canvas.winfo_width(), self._canvas.winfo_height()
188+
189+
def __resized(self, *args):
190+
'''Callback to the window resize events'''
191+
w, h = self.__get_canvas_shape()
192+
# Canvas' size changed, thus we update the object position accordingly
193+
if self._canvas_w != w or self._canvas_h != h:
194+
self._geometry_handler.update_position((w-self._canvas_w)//2, (h-self._canvas_h)//2)
195+
self._canvas_w = w
196+
self._canvas_h = h
197+
self.__changed()
198+
177199
def __changed(self, *args):
178200
self._changed = True
179201

0 commit comments

Comments
 (0)