0

Sorry for mistakes, I am french. I created a window on Tkinter and I used cx_Freeze to make it executable. It works well but my problem is the size of widgets that switch between when it is showed on python as script and showed as executable. It's bigger as executable. I searched on the forum and I found an advice that says: don't force Tkinter'size, it does it very well by itself. But I need choose size on my Labels whatever happens? So I don't understand. The two images are same size, as you can see the difference :executablePython Here is my code to 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")]
)

And my other code for the 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()

Do you have an explanation or a solution please?

Cordially, Thanks

EDIT : When I print the geometry of the window on Python, it shows "1080x650+38+38" and on the Executable it shows "1080x650+362+19". I don't know where the difference come from.

EDIT 2 : Ok, the difference between "1080x650+38+38" and "1080x650+362+19" come from the position of the window on the screen. If you put the window on the up and left corner, you have "1080x650+0+0". So it's not interesting in my problem...

Tesla
  • 3
  • 2
  • you have a frame with height=720 and the summary height for one column seems more than it is.(350 + 350 + 50 + 25) – apet May 05 '20 at 17:08
  • Also in the third line, you have the height equal to 850, fenetre.geometry("1080x850"), maybe it should be 720, and my previous comment may be wrong – apet May 05 '20 at 17:23
  • Thanks, I changed the value of height's canvas and height's rectangles. Now it's like that : fond1=Canvas(frame_bas, bg='#4065A4', height=300, width=400, bd=0, highlightthickness=0) fond2=Canvas(frame_bas, bg='#4065A4', height=300, width=400, bd=0, highlightthickness=0) fond3=Canvas(frame_bas, bg='#4065A4', height=300, width=400, bd=0, highlightthickness=0) fond4=Canvas(frame_bas, bg='#4065A4', height=300, width=400, bd=0, highlightthickness=0) But it' changes nothing, it's again bigger as executable. – Tesla May 05 '20 at 17:50
  • And rectangles :fond1.create_rectangle(0, 0, 400, 300, fill="red", width=5,outline="black") fond2.create_rectangle(0, 0, 400, 300, fill="green", width=5,outline="black") fond3.create_rectangle(0, 0, 400, 300, fill="blue", width=5,outline="black") fond4.create_rectangle(0, 0, 400, 300, fill="purple", width=5,outline="black") – Tesla May 05 '20 at 17:52
  • and what about my second comment: fenetre.geometry("1080x850"), change 850 to 720, it's 3rd line in your code – apet May 05 '20 at 17:53
  • Sorry, I changed also, now it's : fenetre.geometry("1080x720") But always nothing – Tesla May 05 '20 at 17:58
  • I've checked again and on my side the executable geometry match to the tkinter/python geometry. Your code works properly for me. The only suggestion I have for now it's decrease size for 'fond' canvas to 250x300 and set height=30 for 'can1'. Decrease total height to 650. – apet May 06 '20 at 06:31
  • Thanks, I changed what you said but always nothing... I asked a friend to make the executable with my code and it works for him also. Two against one... Maybe my problem is about the make of the executable. – Tesla May 06 '20 at 08:34
  • Sorry, that I can't help you. Good Luck!!! – apet May 06 '20 at 08:46
  • based on your second edit, it means that your executable has correct dimensions 1080x650, as it should be, so maybe it means that there is no problem in code totally, and the question is why the system shows it bigger than it is? – apet May 06 '20 at 11:31
  • Yes exactly, but unfortunately I have no idea of what to do in this case... – Tesla May 06 '20 at 11:41
  • please check these issues, maybe there is an answer: https://stackoverflow.com/questions/36381225/tkinter-not-recognizing-screen-resolution-correctly https://stackoverflow.com/questions/47895320/python-tkinter-not-detecting-correct-screen-resolution-to-use-for-gui-sizing – apet May 06 '20 at 13:54
  • I found!!! Thank you very much. I don't understand very much but i start writing the Answer – Tesla May 06 '20 at 16:57
  • You did it! Welcome! – apet May 06 '20 at 17:12
  • Thanks :) If you know how to explain briefly don't hesitate. And if I can reward you by giving a like or I don't know why tell me, cause you really helped me – Tesla May 06 '20 at 17:16

2 Answers2

0

The code that works only add two sentences at the begining :

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

It seems to be a problem of High DPI Desktop Application, the documentation is this one : Documentation's link Changing size of widgets changed nothing for us.

Tesla
  • 3
  • 2
0

My comment is a bit long, so I'm writing it here.

Most of the monitors have DPI equal to 96 pixels to an inch, but some of them have bigger DPI, high DPI monitors. So, as there are more pixels on inch and application widgets and text are measured in pixel, it means that everything becomes smaller for that kind of monitors. To correct it and adapt a picture, make it a comfortable size for the user, Microsoft uses DPI-awareness-mode. The DPI awareness should be set on the application side to let windows know how to scale the app's picture/widgets. With the command

    ctypes.windll.shcore.SetProcessDpiAwareness(2)

we set awareness, the 2 is a factor, means 200%, 100% is 96pixel per inch, so 2 is 192 pixels.

For a more correct explanation please check docs. Welcome for any correction/edition.

apet
  • 958
  • 14
  • 16