我正在尝试找到一个公式来缩放按钮的字体大小以保留所有比例。
起初我尝试用它来缩放我的小部件,效果很好。我使用以下符号来说明我的方法:
通过两个屏幕的最后两个公式,我可以根据给出的长度l_x,l'_x获得像素数量
为了保持第一个屏幕在第二个屏幕上的比例,我必须考虑
使用公式 (1),(2) 和 (3) 我得到了 P_l_x' 的新关系,由
给出以此类推,我在垂直方向上得到相同的结果
有了这些知识,我可以在以下代码中为我使用的小部件获得适当的缩放:
import tkinter as tk
from screeninfo import get_monitors
# First monitor informations with a display resolution of 1920x1080
DPI_b = 102.23899371069182 # dots per inch
b = 477 # in mm
DPI_h = 102.35820895522387 # dots per inch
h = 268 # in mm
# Second monitor informations
monitors = get_monitors()
monitor = monitors[0]
b_ = monitor.width_mm
h_ = monitor.height_mm
#print(b_, h_)
P_b_ = monitor.width
P_h_ = monitor.height
DPI_b_ = P_b_ / b_ * 2.54*10
DPI_h_ = P_h_ / h_ * 2.54*10
#print(DPI_b_, DPI_h_)
A_b = DPI_b_/DPI_b
B_b = b_/b
A_h = DPI_h_/DPI_h
B_h = h_/h
print(A_b, B_b, A_h, B_h)
root = tk.Tk()
formatierung = str(int(1370*A_b*B_b)) + 'x' + str(int(120*A_h*B_h))
print(formatierung)
root.geometry(formatierung)
screen_width = root.winfo_screenwidth()
screen_height = root.winfo_screenheight()
print(screen_width, screen_height)
for i in range(8):
button = tk.Button(master = root, font=('Arial', int(12*screen_height*screen_width/(1080*1920))), text = 'Bestaetigen fuer ' + str(i+1))
button.place(x = int((10+170*i)*A_b*B_b), y = int(10*A_h*B_h), width = int(160*A_b*B_b), height = int(32*A_h*B_h))
for i in range(8):
button = tk.Button(master = root, font=('Arial', int(12*screen_height*screen_width/(1080*1920))), text = 'Bestaetigen fuer ' + str(i+9))
button.place(x = int((10+170*i)*A_b*B_b), y = int(60*A_h*B_h), width = int(160*A_b*B_b), height = int(32*A_h*B_h))
root.mainloop()
从这里我测试了不同的显示分辨率。我注意到以下几点:
1.) 按钮被正确调用。
2.) 字体大小似乎调整错误。
据我所知,tkinter 中的字体大小的单位为 pt,它由字母的高度定义,通过更改 pt,宽度会按比例调整。但无论我到目前为止尝试过什么,例如
int(12 * 屏幕高度 * 屏幕宽度/(1080*1920))
我总是收到显示分辨率的示例,其中小部件上的字母太大(或太小)。 那么,真正需要哪些依赖才能在另一个屏幕上获得正确的 pt 值呢?
我的近似解:
import tkinter as tk
from screeninfo import get_monitors
# First monitor informations
DPI_b = 102.23899371069182 # dots per inch
b = 477 # in mm
DPI_h = 102.35820895522387 # dots per inch
h = 268 # in mm
# Second monitor informations
monitors = get_monitors()
monitor = monitors[0]
b_ = monitor.width_mm
h_ = monitor.height_mm
#print(b_, h_)
P_b_ = monitor.width
P_h_ = monitor.height
DPI_b_ = P_b_ / b_ * 2.54*10
DPI_h_ = P_h_ / h_ * 2.54*10
#print(DPI_b_, DPI_h_)
A_b = DPI_b_/DPI_b
B_b = b_/b
A_h = DPI_h_/DPI_h
B_h = h_/h
print(A_b, B_b, A_h, B_h)
root = tk.Tk()
formatierung = str(int(1370*A_b*B_b)) + 'x' + str(int(120*A_h*B_h))
print(formatierung)
root.geometry(formatierung)
screen_width = root.winfo_screenwidth()
screen_height = root.winfo_screenheight()
# >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> new <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
# Find the best font size k
ratio = 141/160 # 141 used pixels for text = 'Bestaetigen fuer ' + str(0), 160 used pixels for whole button
k = 0
button = tk.Button(master = root, font=('Arial', k+1), text = 'Bestaetigen fuer ' + str(0))
pixel_aktuell = button.winfo_reqwidth()
pixel_breite = int(160*A_b*B_b)
error = abs(ratio-pixel_aktuell/pixel_breite)
E = [error]
k = 1
button = tk.Button(master = root, font=('Arial', k+1), text = 'Bestaetigen fuer ' + str(0))
pixel_aktuell = button.winfo_reqwidth()
pixel_breite = int(160*A_b*B_b)
error = abs(ratio-pixel_aktuell/pixel_breite)
E.append(error)
while E[k-1] >= E[k]:
k += 1
button = tk.Button(master = root, font=('Arial', k+1), text = 'Bestaetigen fuer ' + str(0))
pixel_aktuell = button.winfo_reqwidth()
pixel_breite = int(160*A_b*B_b)
error = abs(ratio-pixel_aktuell/pixel_breite)
E.append(error)
print(k)
# >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> new <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
print(screen_width, screen_height)
for i in range(8):
button = tk.Button(master = root, font=('Arial', k), text = 'Bestaetigen fuer ' + str(i+1))
button.place(x = int((10+170*i)*A_b*B_b), y = int(10*A_h*B_h), width = int(160*A_b*B_b), height = int(32*A_h*B_h))
print(button.winfo_reqwidth())
for i in range(8):
button = tk.Button(master = root, font=('Arial', k), text = 'Bestaetigen fuer ' + str(i+9))
button.place(x = int((10+170*i)*A_b*B_b), y = int(60*A_h*B_h), width = int(160*A_b*B_b), height = int(32*A_h*B_h))
print(button.winfo_reqwidth())
root.mainloop()