Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Tkinter window not opening, but if I comment out a line, it works

So I was trying to create a GUI in pygame, but after a while, I just decided to re-write it in tkinter. I need to create 3 bars, one being a canvas, the other 2 being empty. The size of the left bar is leftbarwidth and the right rightbarwidth, set to 200 and 300 respectively. So far I have this (The logic of the program not included)

 1 from tkinter import *
 2 
 3 def windowsizeupdate(event):
 4     preview.config(width=event.width-leftbarwidth-rightbarwidth, height=event.height)
 5 
 6 root = Tk()
 7 root.title("TITLE HERE")
 8 root.geometry(f"{leftbarwidth + rightbarwidth + 200}x600+{int(screen.winfo_screenwidth() / 2 - (leftbarwidth + rightbarwidth + 200) / 2)}+{int(screen.winfo_screenheight() / 2 - 300)}")
 9 root.minsize(800, 600)
10 root.attributes("-topmost", 1)
11
12 preview = Canvas(root, bd=0, bg="#800000", cursor="dot", width=200, height=600)
13 preview.place(x=leftbarwidth, y=0)
14
15 root.bind("<Configure>",windowsizeupdate)
16 root.mainloop()

But whenever I run it, I just get this:

app not responding mac

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

After commenting out line 4, and replacing it with print(event.width-leftbarwidth-rightbarwidth, event.height, leftbarwidth), I discovered
everything works, and all the values should work with the config function!

But if I put in this:

 1 from tkinter import *
 2 
 3 def windowsizeupdate(event):
 4     print(event.width-leftbarwidth-rightbarwidth, event.height, leftbarwidth)
 5     preview.config(width=event.width-leftbarwidth-rightbarwidth, height=event.height)
 6 
 7 root = Tk()
 8 root.title("TITLE HERE")
 9 root.geometry(f"{leftbarwidth + rightbarwidth + 200}x600+{int(screen.winfo_screenwidth() / 2 - (leftbarwidth + rightbarwidth + 200) / 2)}+{int(screen.winfo_screenheight() / 2 - 300)}")
10 root.minsize(800, 600)
11 root.attributes("-topmost", 1)
12
13 preview = Canvas(root, bd=0, bg="#800000", cursor="dot", width=200, height=600)
14 preview.place(x=leftbarwidth, y=0)
15
16 root.bind("<Configure>",windowsizeupdate)
17 root.mainloop()

I see my console output looks like this:

long console output
Can someone explain this to me!

>Solution :

Because when you resize the window, that raises another <Configure> event and recalls your handler. You need to detect that the event wat not triggered by your change:

def windowsizeupdate(event):
     if event.x != leftbarwidth:
        preview.config(width=event.width-leftbarwidth-rightbarwidth, 
height=event.height)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading