0
如何在tkinter中创建应用程序,以适应适合较小PC尺寸的文本大小和尺寸?例如,在其他语言中,您可以指定文本大小和尺寸为百分比。有没有办法在Python 3中做到这一点?我正在使用tkinter作为我的GUI工具包Tkinter自适应文本和尺寸
如何在tkinter中创建应用程序,以适应适合较小PC尺寸的文本大小和尺寸?例如,在其他语言中,您可以指定文本大小和尺寸为百分比。有没有办法在Python 3中做到这一点?我正在使用tkinter作为我的GUI工具包Tkinter自适应文本和尺寸
一种解决方案是make a theme or styles,您应用了自己设置的缩放算法。这里只是一个简单的例子,只有一个按钮。
import tkinter as tk
from tkinter import ttk
root = tk.Tk()
# Base size
normal_width = 1920
normal_height = 1080
# Get screen size
screen_width = root.winfo_screenwidth()
screen_height = root.winfo_screenheight()
# Get percentage of screen size from Base size
percentage_width = screen_width/(normal_width/100)
percentage_height = screen_height/(normal_height/100)
# Make a scaling factor, this is bases on average percentage from
# width and height.
scale_factor = ((percentage_width + percentage_height)/2)/100
# Set the fontsize based on scale_factor,
# if the fontsize is less than minimum_size
# it is set to the minimum size
fontsize = int(14 * scale_factor)
minimum_size = 8
if fontsize < minimum_size:
fontsize = minimum_size
# Create a style and configure for ttk.Button widget
default_style = ttk.Style()
default_style.configure('New.TButton', font=("Helvetica", fontsize))
frame = ttk.Frame(root)
button = ttk.Button(frame, text="Test", style='New.TButton')
frame.grid(column=0, row=0)
button.grid(column=0, row=0)
root.mainloop()
这将取决于你使用什么类型的小部件,如何将其应用到你的GUI。例如tk.Button与ttk.Button不同。从我所了解的ttk小部件来说,当涉及本地外观比tk小部件好,但我没有自己测试过。
要将其应用于完整的GUI,您需要查看GUI中使用的不同小组件,并查看可以在哪里进行缩放。你需要调整scale_factor来让它看起来不错。您可以通过将screen_width和screen_height设置为您自己选择的分辨率来测试不同的“屏幕尺寸”。
这里是一个风格和主题tutorial from TkDocs
你问一个标签,或整个GUI,如文字处理或表格要填写? –
我是指GUI窗口中的所有元素。文本大小,框架宽度,输入框宽度,图像大小,e.t.c。当我使用应用程序让我们说一个迷你PC时,只有它的一部分适合这个小屏幕。我希望它能够适应,并且无论屏幕大小如何都适合一切。 –