Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/336.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python tkinter中的字体大小与tkinter中的窗口大小/像素密度因子/原始屏幕分辨率之间有什么关系_Python_Python 3.x_User Interface_Tkinter_Window - Fatal编程技术网

Python tkinter中的字体大小与tkinter中的窗口大小/像素密度因子/原始屏幕分辨率之间有什么关系

Python tkinter中的字体大小与tkinter中的窗口大小/像素密度因子/原始屏幕分辨率之间有什么关系,python,python-3.x,user-interface,tkinter,window,Python,Python 3.x,User Interface,Tkinter,Window,这个问题一直困扰着我,我似乎不明白为什么它不能正确地扩展到每个设备 我已经在3台不同的电脑(2台笔记本电脑和一台安卓平板电脑)上运行了我的代码,但我不知道发生了什么 我还使用了比例因子(dpi/72)。显示的是原始的比例因子,但我使用以下代码使比例因子与我的主要笔记本电脑比例因子成比例: dpi = root.winfo_fpixels('1i') factor = dpi / 72 factor_multiplier = (.40*factor) +.46 factor = factor/fa

这个问题一直困扰着我,我似乎不明白为什么它不能正确地扩展到每个设备

我已经在3台不同的电脑(2台笔记本电脑和一台安卓平板电脑)上运行了我的代码,但我不知道发生了什么

我还使用了比例因子(dpi/72)。显示的是原始的比例因子,但我使用以下代码使比例因子与我的主要笔记本电脑比例因子成比例:

dpi = root.winfo_fpixels('1i')
factor = dpi / 72
factor_multiplier = (.40*factor) +.46
factor = factor/factor_multiplier
starting_frame.tk.call('tk', 'scaling', factor)

主计算机:

屏幕大小:1920x1980,窗口大小:1280x960,比例因子:1.3,正确字体:40

其他:

屏幕大小:1366x768,窗口大小:1912x683,比例因子:1.3,正确字体:30

屏幕大小:2560x1600,窗口大小:1750x1311,比例因子:5.7,正确字体:30

每个窗口的大小约为实际屏幕分辨率的88%,并大致转换为4:3

我不明白为什么我的另外两台笔记本电脑可以有如此不同的屏幕分辨率+dpi,但仍然需要相同的字体大小。此外,dpi与我的主要笔记本电脑成比例,因此这可能也是一个因素。所有字体“适合”窗口,但它们并不都适合按比例制作的框(即,对于所有屏幕分辨率,框的比例相同,如下面完整的相关代码所示)

相关代码如下:

def font_size_calc(): #this is the problematic code that isnt scaling the font size right
    global r2_width, r2_height
    x = r2_width*int(r2_height)
    return str(math.ceil((-(1/106545)*x) + 51.533155004927)) #linear equation I made, slope = fontsize/screen's area
    '''
    I tried creating a linear equation based on the fonts that worked for
    2 of my devices, but a device with a smaller screen resolution than both of my devices needs a font size of 30 like my highest res device
    to have the words stay in the box
    '''
starting_frame.create_text(adjusted_pixels(2,"x"),adjusted_pixels(2.742857,"y"),
    text = "Enter number of laps",fill = "gray", font=("Calibri "+ font_size_calc())) #calls font_size_calc() to get a working font size
    #I want the text to fit in the box created by the var border, it works on my laptop and tablet, but not another old laptop with a smaller screen res
    #obviously my equation is wrong but im not sure how to scale the font to work in all cases
程序的代码(经过编辑,您也可以运行它):

将tkinter作为tk导入
从tkinter.ttk导入*
输入数学
从PIL导入ImageTk,图像
root=tk.tk()
dpi=root.winfo_fpixels('1i')
factor=dpi/72#获取dpi比标准72大多少倍,用于使屏幕小部件成比例
width=root.winfo_screenwidth()
高度=root.winfo_屏幕高度()
比率_1=宽度*高度
比率_2=1#init
r2_width=4#将窗口设置为4/3分辨率
r2_高度=3
而(比率1/比率2)!=1.6875:#确保新窗面积为旧窗面积的88%
r2_高度=r2_宽度/1.333333333333
ratio_2=r2_宽度*r2_高度#获取新屏幕大小的区域
r2_width=r2_width+1#将宽度增加1

如果(ratio_1/ratio_2)我的问题是:我是根据屏幕窗口计算字体的,而不是全屏大小。这是因为我的大小不正确。

您是否知道
dpi=root.winfo_fpixels('1i')
使用比例因子来确定dpi,并且可能无法为您的系统准确设置比例因子?换句话说,我认为您的
dpi
计算不能保证是正确的。如果
dpi
值错误,则所有其他计算都将错误。@BryanOakley不是变量
factor
只是查找
dpi=root.winfo\u fpixels('1i')
使用的比例因子吗?所以这不重要,对吗?Nvm我明白你的意思,我手动运行了计算,结果是错误的(我以前这么做过,我认为它是正确的)
import tkinter as tk
from tkinter.ttk import *
import math
from PIL import ImageTk,Image

root = tk.Tk()
dpi = root.winfo_fpixels('1i')
factor = dpi / 72 #gets how many times biger the dpi is than the standard 72, used for making the screen widgets proportional later
width = root.winfo_screenwidth()
height = root.winfo_screenheight()


ratio_1 = width * height
ratio_2 = 1 #init
r2_width = 4 #setting the window to a 4/3 resolution
r2_height = 3

while (ratio_1 / ratio_2) != 1.6875: #making sure that the new window is 88% the area of the old window
    r2_height = r2_width / 1.333333333333333333333333333333333333333333333333333333 #gets hieght
    ratio_2 = r2_width * r2_height # gets the area of the new screen size
    r2_width = r2_width + 1 #increases the width by one

    if (ratio_1/ratio_2) <= 1.6875: #makes sure it doesnt go too far over
        break
    if width + 1 == r2_width: #another check to make sure it doesnt keep going
        break

print("{} x {} = {}".format(str(r2_width),str(r2_height),str(ratio_1/ratio_2)))#prints your ending resolution to manually check accuracy

root.geometry(str(r2_width) + "x"+ str(int(r2_height))) #sets window resolution
starting_frame = tk.Canvas(root) #makes a frame

factor_multiplier = (.40*factor) +.46 #makes a multiplier based on an equation I cam up with (im so glad it was linear)
factor = factor/factor_multiplier #sets factor to be this new peoportion
starting_frame.tk.call('tk', 'scaling', factor)#adjusts tkinter scaling factor for the pixels
starting_frame.place(height=int(r2_height), width = r2_width) #places the frame




images = []
def create_rectangle(frame, x1, y1, x2, y2, **kwargs): #this works so dont worry about this function
    if 'alpha' in kwargs:
        alpha = int(kwargs.pop('alpha') * 255)
        fill = kwargs.pop('fill')
        fill = root.winfo_rgb(fill) + (alpha,)
        image = Image.new('RGBA', (x2-x1, y2-y1), fill)
        images.append(ImageTk.PhotoImage(image))
        frame.create_image(x1, y1, image=images[-1], anchor='nw')
    return frame.create_rectangle(x1, y1, x2, y2, **kwargs)

def adjusted_pixels(number, x_or_y): #this code creates new coordinates based on set numbers I ahve already made and works aswell
    global r2_width, r2_height

    if x_or_y == 'x':
        print(int(r2_width / number))
        return(int(r2_width / number))
    else:
        print(int(r2_height / number))
        return(int(r2_height / number))

def font_size_calc(): #this is the problematic code that isnt scaling the font size right
    global r2_width, r2_height
    x = r2_width*int(r2_height)
    return str(math.ceil((-(1/106545)*x) + 51.533155004927)) #linear equation I made, slope = fontsize/screen's area
    '''
    I tried creating a linear equation based on the fonts that worked for
    2 of my devices, but a device with a smaller screen resolution than both of my devices needs a font size of 30 like my highest res device
    to have the words stay in the box
    '''


boarder = create_rectangle(starting_frame,adjusted_pixels(3.368421,"x"), adjusted_pixels(3.2,'y'),
    adjusted_pixels(1.4222222,"x"), adjusted_pixels(1.45454545,"y"), fill='black',alpha = .78,outline = "red") #creates a box thats slightly transparent, works fine
lap_text = starting_frame.create_text(adjusted_pixels(2,"x"),adjusted_pixels(2.742857,"y"),
    text = "Enter number of laps",fill = "gray", font=("Calibri "+ font_size_calc())) #calls font_size_calc() to get a working font size
    #I want the text to fit in the box created by the var border, it works on my laptop and tablet, but not another old laptop with a smaller screen res
    #obviously my equation is wrong but im not sure how to scale the font to work in all cases

root.mainloop()