带有tkinter窗口和cx_Freeze的小部件尺寸变化

问题描述 投票:0回答:1

对不起,我是法国人。我在Tkinter上创建了一个窗口,并使用cx_Freeze使其可执行。它运作良好,但我的问题是在python以脚本形式显示和以可执行文件形式显示之间切换的小部件的大小。作为可执行文件更大。我在论坛上搜索时发现一条建议:不要强迫Tkinter'size,它本身会做得很好。但是无论发生什么情况,我都需要在标签上选择尺寸?所以我不明白。如您所见,两张图片的大小相同: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")]
)

以及窗口的其他代码:

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()

请问您有解释或解决方法吗?

Cordily谢谢

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

编辑2:好的,“ 1080x650 + 38 + 38”和“ 1080x650 + 362 + 19”之间的差异来自屏幕上窗口的位置。如果将窗口放在左上角,则“ 1080x650 + 0 + 0”。所以这对我的问题没有兴趣...

python tkinter widget cx-freeze
1个回答
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更改小部件的大小对我们没有任何改变。

© www.soinside.com 2019 - 2024. All rights reserved.