Skip to content

Commit e098356

Browse files
Fix window size according to system font size (close #11)
1 parent bbec991 commit e098356

File tree

5 files changed

+59
-22
lines changed

5 files changed

+59
-22
lines changed

bin/font_size.py

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
from tkinter import font
2+
3+
FONT_DEFAULT_SIZE = 9
4+
FONT_DEFAULT_SIZE_PIXEL = -12
5+
6+
def get_font_scale_factor(font_name):
7+
"""
8+
Calculate the ratio between the system font and the default font, on which default sizes are based
9+
:return the ratio between system font (currently used) and the default font
10+
"""
11+
font_size_system = font.nametofont(font_name).cget("size")
12+
13+
if font_size_system > 0:
14+
# pt size
15+
return font_size_system / FONT_DEFAULT_SIZE
16+
else:
17+
return font_size_system / FONT_DEFAULT_SIZE_PIXEL
18+

bin/gui.py

+10-7
Original file line numberDiff line numberDiff line change
@@ -10,19 +10,19 @@
1010
from bin.vpn_util.vpn import *
1111
from bin.gui_components.settings_frame import SettingsFrame
1212
from bin.gui_components.advanced_settings_window import DEFAULT_SCALE_FACTOR, DEFAULT_NM_USE
13+
from bin.font_size import get_font_scale_factor
1314
import threading
1415

1516
logger = get_logger(__name__)
1617

1718
DEFAUL_WIDTH = 370
1819
DEFAUL_HEIGHT = 340
1920

20-
2121
class gui(Tk):
2222
def __init__(self):
2323
super().__init__()
2424
self.wm_title("NordPY")
25-
25+
2626
# sets the icon
2727
self.__imgicon__ = PhotoImage(file=os.path.join(CURRENT_PATH+"media", "nordvpn.png"))
2828
self.tk.call('wm', 'iconphoto', self._w, self.__imgicon__)
@@ -34,15 +34,15 @@ def __init__(self):
3434
else:
3535
(self.scale_factor, self.nm_use) = (DEFAULT_SCALE_FACTOR, DEFAULT_NM_USE)
3636

37-
self.center_window(DEFAUL_WIDTH, DEFAUL_HEIGHT, self.scale_factor)
38-
3937
self.settings_frame = SettingsFrame(self, self.scale_factor)
4038
self.manual_frame = ManualSelectionFrame(self, self.background_color, self.scale_factor)
4139
self.optionsFrame = OptionFrame(self)
4240
self.__init_protocol__()
4341
self.__initStatus__()
4442
self.__initButtons__()
4543

44+
self.center_window(DEFAUL_WIDTH, DEFAUL_HEIGHT, self.scale_factor, self.optionsFrame.cget("font"))
45+
4646
running_vpn = get_running_vpn()
4747
if running_vpn is not None:
4848
self.setStatusAlreadyConnected(running_vpn)
@@ -251,13 +251,16 @@ def reset_settings(self):
251251
def update_advanced_settings(self, nm_use):
252252
self.nm_use = nm_use
253253

254-
def center_window(self, width=300, height=200, scale=DEFAULT_SCALE_FACTOR):
254+
def center_window(self, width=300, height=200, scale=DEFAULT_SCALE_FACTOR, font_name="TkDefaultFont"):
255255
# gets screen width and height
256256
screen_width = self.winfo_screenwidth()
257257
screen_height = self.winfo_screenheight()
258258

259-
scaled_width = width * scale
260-
scaled_height = height * scale
259+
font_factor = get_font_scale_factor(font_name)
260+
logger.info("Font factor: " + str(font_factor))
261+
262+
scaled_width = width * scale * font_factor
263+
scaled_height = height * scale * font_factor
261264

262265
# calculates position x and y coordinates
263266
x = (screen_width / 2) - (scaled_width / 2)

bin/gui_components/advanced_settings_window.py

+8-5
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
from bin.credentials import credentials_file_path
55
from bin.settings import advanced_settings_are_correct, advanced_settings_read, advanced_settings_save
66
from bin.root import get_root_permissions
7+
from bin.font_size import get_font_scale_factor
78

89
DEFAULT_SCALE_FACTOR = 1
910
DEFAULT_NM_USE = False
@@ -39,7 +40,7 @@ def __init__(self, main_gui, scale_factor=1):
3940
self.set_scale(DEFAULT_SCALE_FACTOR)
4041
self.set_nm_use(DEFAULT_NM_USE)
4142

42-
self.center_window(300 * scale_factor, 150 * scale_factor)
43+
self.center_window(300 * scale_factor, 150 * scale_factor, self.save_button.cget("font"))
4344

4445
self.grab_set() # used to disable the underlying window
4546

@@ -95,12 +96,14 @@ def get_nm_use(self):
9596
def set_nm_use(self, use):
9697
self.nm_use.set(use)
9798

98-
def center_window(self, width=300, height=200):
99+
def center_window(self, width=300, height=200, font_name='TkDefaultFont'):
99100
# gets screen width and height
100101
screen_width = self.winfo_screenwidth()
101102
screen_height = self.winfo_screenheight()
102103

104+
font_factor = get_font_scale_factor(font_name)
105+
103106
# calculates position x and y coordinates
104-
x = (screen_width / 2) - (width / 2)
105-
y = (screen_height / 2) - (height / 2)
106-
self.geometry('%dx%d+%d+%d' % (width, height, x, y))
107+
x = (screen_width / 2) - (width * font_factor / 2)
108+
y = (screen_height / 2) - (height * font_factor / 2)
109+
self.geometry('%dx%d+%d+%d' % (width * font_factor, height * font_factor, x, y))

bin/gui_components/manual_selection_window.py

+12-5
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
from bin.conf_util import get_available_servers_dict, global_stats_holder
55
from bin.logging_util import get_logger
66
from bin.pathUtil import CURRENT_PATH
7+
from bin.font_size import get_font_scale_factor
78
from requests import ConnectionError as RequestsConnectionError
89

910
SERVERS_DICT = get_available_servers_dict()
@@ -20,10 +21,11 @@ def __init__(self, parent, scale_factor=1):
2021
self.__imgicon__ = PhotoImage(file=path.join(CURRENT_PATH + "media", "manual.png"))
2122
self.tk.call('wm', 'iconphoto', self._w, self.__imgicon__)
2223

23-
self.center_window(300 * scale_factor, 190 * scale_factor)
2424
self.__init_listboxes__()
2525
self.__init_buttons__()
2626

27+
self.center_window(300 * scale_factor, 190 * scale_factor, self.accept_button.cget("font"))
28+
2729
self.grab_set() # used to disable the underlying window
2830

2931
# creating stats holder
@@ -102,12 +104,17 @@ def cancel_pressed(self):
102104
self.grab_release() # to enable again the underlying window
103105
self.destroy()
104106

105-
def center_window(self, width=300, height=200):
107+
def center_window(self, width=300, height=200, font_name='TkDefaultFont'):
106108
# gets screen width and height
107109
screen_width = self.winfo_screenwidth()
108110
screen_height = self.winfo_screenheight()
111+
112+
font_factor = get_font_scale_factor(font_name)
113+
114+
scaled_width = width * font_factor
115+
scaled_height = height * font_factor
109116

110117
# calculates position x and y coordinates
111-
x = (screen_width / 2) - (width / 2)
112-
y = (screen_height / 2) - (height / 2)
113-
self.geometry('%dx%d+%d+%d' % (width, height, x, y))
118+
x = (screen_width / 2) - (scaled_width / 2)
119+
y = (screen_height / 2) - (scaled_height / 2)
120+
self.geometry('%dx%d+%d+%d' % (scaled_width, scaled_height, x, y))

bin/gui_components/root_password_window.py

+11-5
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import os
33
from bin.pathUtil import CURRENT_PATH
44
from bin.root import test_root_password, wrong_root_password
5+
from bin.font_size import get_font_scale_factor
56

67
password_inserted = None
78

@@ -40,7 +41,7 @@ def __init__(self):
4041
# binding enter button to on_button_pressed function
4142
self.bind('<Return>', self.on_enter_pressed)
4243

43-
self.center_window(200, 80)
44+
self.center_window(200, 80, self.ok_button.cget("font"))
4445

4546
def on_enter_pressed(self, event):
4647
self.on_button_pressed()
@@ -58,13 +59,18 @@ def on_button_pressed(self):
5859
wrong_root_password()
5960
self.password_field.delete(0, END)
6061

61-
def center_window(self, width=300, height=200):
62+
def center_window(self, width=300, height=200, font_name='TkDefaultFont'):
6263
# gets screen width and height
6364
screen_width = self.winfo_screenwidth()
6465
screen_height = self.winfo_screenheight()
66+
67+
font_factor = get_font_scale_factor(font_name)
68+
69+
scaled_height = height * font_factor
70+
scaled_width = width * font_factor
6571

6672
# calculates position x and y coordinates
67-
x = (screen_width / 2) - (width / 2)
68-
y = (screen_height / 2) - (height / 2)
69-
self.geometry('%dx%d+%d+%d' % (width, height, x, y))
73+
x = (screen_width / 2) - (scaled_width / 2)
74+
y = (screen_height / 2) - (scaled_height / 2)
75+
self.geometry('%dx%d+%d+%d' % (scaled_width, scaled_height, x, y))
7076

0 commit comments

Comments
 (0)