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

Time:01-21

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

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!

CodePudding user response:

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)

CodePudding user response:

If your goal is to keep the left and right side windows to be a fixed size, you don't need to do that via a binding on the <Configure> event. This sort of thing is exactly what the geometry managers pack, place, and grid are for.

You're using place which makes this problem harder to solve since it's difficult to have the width of a widget to fill the space between other widgets. A better solution is to use grid or pack.

For example, using grid you can set a minsize for the left and right columns and then let the middle column fill all of the additional space.

from tkinter import *

leftbarwidth = 200
rightbarwidth = 300

root = Tk()
root.minsize(800, 600)
preview = Canvas(root, bd=0, bg="#800000", cursor="dot", width=200, height=600)

root.grid_columnconfigure(0, minsize=leftbarwidth)
root.grid_columnconfigure(2, minsize=rightbarwidth)
root.grid_columnconfigure(1, weight=1)
root.grid_rowconfigure(0, weight=1)
preview.grid(row=0, column=1, sticky="nsew")

root.mainloop()

screenshot

  •  Tags:  
  • Related