tkinter window 和 cx_Freeze 的小部件大小变化

Widget size variation with tkinter window and cx_Freeze

对不起,我是法国人。 我在 Tkinter 上创建了一个 window 并使用 cx_Freeze 使其可执行。 它运行良好,但我的问题是小部件的大小在 python 上显示为脚本和显示为可执行文件时切换。它比可执行文件大。我在论坛上搜索了一下,发现了一条建议:不要强制 Tkinter 的大小,它自己做得很好。但无论发生什么情况,我都需要在我的标签上选择尺寸?所以我不明白。两张图大小一样,可以看出区别:executablePython 这是我的代码 cx_Freeze:

from cx_Freeze import setup, Executable

setup(
    name="Mon super programme",
    version="0.1",
    description="Affiche le path",
    executables=[Executable("Accueil generateur et correcteur.py")]
)

还有我的 window 的其他代码:

from tkinter import *
fenetre=Tk()
fenetre.geometry("1080x850")
fenetre.config(bg='#4065A4')

###Frame
cadre=Frame(fenetre, width=1080, height=720, borderwidth=0, bg='#4065A4')
frame_bas=Frame(cadre, borderwidth=0, bg='#4065A4')

###Canvas
can1=Canvas(frame_bas, bg='#4065A4', height=50, width=1, bd=0, highlightthickness=0)   #Espace
can2=Canvas(frame_bas, bg='#4065A4', height=25, width=1, bd=0, highlightthickness=0)   #Espace
can3=Canvas(frame_bas, bg='#4065A4', height=1, width=25, bd=0, highlightthickness=0)   #Espace
fond1=Canvas(frame_bas, bg='#4065A4', height=350, width=400, bd=0, highlightthickness=0)
fond2=Canvas(frame_bas, bg='#4065A4', height=350, width=400, bd=0, highlightthickness=0)
fond3=Canvas(frame_bas, bg='#4065A4', height=350, width=400, bd=0, highlightthickness=0)
fond4=Canvas(frame_bas, bg='#4065A4', height=350, width=400, bd=0, highlightthickness=0)

###Formes
fond1.create_rectangle(0, 0, 400, 350, fill="red", width=5,outline="black")
fond2.create_rectangle(0, 0, 400, 350, fill="green", width=5,outline="black")
fond3.create_rectangle(0, 0, 400, 350, fill="blue", width=5,outline="black")
fond4.create_rectangle(0, 0, 400, 350, fill="purple", width=5,outline="black")

###Labels
titre=Label(cadre, text="Générateur et correcteur de QCM", font=('Arial',25), bg='#4065A4')
corriger=Label(frame_bas, text="Corriger une\n épreuve", font=('Arial',25), bg='green')
creer=Label(frame_bas, text="Créer une base\n de questions", font=('Arial',25), bg='red')
generer=Label(frame_bas, text="Générer une\n épreuve \n à partir \nd'une base", font=('Arial',25), bg='blue')
modifier=Label(frame_bas, text="Modifier une\n base de \nquestions", font=('Arial',25), bg='purple')

###Intégration au Frame
titre.grid(row=0, column=0)
can1.grid(row=0, column=0)
can2.grid(row=2, column=0)
can3.grid(row=1, column=1)
corriger.grid(row=1, column=0)
fond2.grid(row=1, column=0)
creer.grid(row=1, column=2)
fond1.grid(row=1, column=2)
generer.grid(row=3, column=0)
fond3.grid(row=3, column=0)
modifier.grid(row=3, column=2)
fond4.grid(row=3, column=2)
frame_bas.grid(row=1, column=0)

###Fonctions action\réaction
def afficher1(event):
    print("créer")
def afficher2(event):
    print(fenetre.geometry())
def afficher3(event):
    print("générer")
def afficher4(event):
    print("modifier")

###Action\Réaction
fond1.bind('<Button-1>', afficher1)
creer.bind('<Button-1>', afficher1)
fond2.bind('<Button-1>', afficher2)
corriger.bind('<Button-1>', afficher2)
fond3.bind('<Button-1>', afficher3)
generer.bind('<Button-1>', afficher3)
fond4.bind('<Button-1>', afficher4)
modifier.bind('<Button-1>', afficher4)



cadre.pack()
fenetre.mainloop()

请问有解释或解决办法吗?

诚挚地, 谢谢

编辑: 当我在 Python 上打印 window 的几何图形时,它显示“1080x650+38+38”,在可执行文件上它显示“1080x650+362+19”。我不知道差异来自哪里。

编辑 2: 好的,“1080x650+38+38”和“1080x650+362+19”之间的区别来自 window 在屏幕上的位置。如果将 window 放在左上角,则为“1080x650+0+0”。所以这对我的问题没有意义...

有效的代码只在开头添加两句:

from tkinter import *
import ctypes
ctypes.windll.shcore.SetProcessDpiAwareness(2) # if your windows version >= 8.1
fenetre=Tk()

好像是High DPI Desktop Application的问题,文档是这个: Documentation's link 更改小部件的大小对我们没有任何影响。

我的评论有点长,所以我写在这里。

大多数显示器的 DPI 等于 96 像素到一英寸,但其中一些具有更大的 DPI、高 DPI 显示器。因此,随着英寸上有更多像素,应用程序小部件和文本以像素为单位进行测量,这意味着对于那种显示器来说一切都变得更小了。 为了更正它并调整图片,使其大小适合用户,Microsoft 使用 DPI 感知模式。应在应用程序端设置 DPI 感知,让 windows 知道如何缩放应用程序的 picture/widgets。 使用命令

    ctypes.windll.shcore.SetProcessDpiAwareness(2)

我们设置awareness,2是一个因子,表示200%,100%是96pixel per inch,所以2是192pixels。

要获得更正确的解释,请查看文档。 欢迎任何 correction/edition.