简体   繁体   中英

Issues with tkinter widgets in the Python turtle graphics window

I have created a program in Python turtle graphics and have created some widgets to put in the window. However, there is one major issue with the widgets: they just stay stuck at the bottom of the window, and I CANNOT move them ANYWHERE ELSE around the screen . They just stay stuck at the bottom window in a line from left to right, and this becomes a real issue when having many widgets. For clarification, here is an image of what I am talking about: 小部件问题

What I want is to be able to move the widgets to the top, left, or right side of the window instead of them just being stuck to the bottom of it. And, as you can see, since I have created A LOT of widgets, the fact that I cannot do this becomes a real issue. So, why is this happening and how would I fix it? Also, just in case, below is the a code block for one of my menus and one of my buttons:

Menu code block:

tb = Menubutton(text = "Change pen thickness")
tb.pack(side = 'left')
tb.menu = Menu(tb, tearoff = 0)
tb["menu"] = tb.menu
thick = tb.menu
thick.add_command(label = "1", command = width1)
thick.add_command(label = "2", command = width2)
thick.add_command(label = "3", command = width3)
thick.add_command(label = "4", command = width4)
thick.add_command(label = "5", command = width5)
thick.add_command(label = "6", command = width6)
thick.add_command(label = "7", command = width7)
thick.add_command(label = "8", command = width8)
thick.add_command(label = "9", command = width9)
thick.add_command(label = "10", command = width10)
tb.pack()

Button code block:

change = Button(text = "Change letter dimensions", command = NewLetterDimensions)
change.pack(side = 'left')

The fact that the widgets are packed to the left is not the issue, as even before I did that, the widgets would stay stuck to the bottom of the window. The only difference was that they were stacked from top to bottom instead of left to right.

Google has not been much help in finding a solution, so I am turning over to SO. Any help is much appreciated!

EDIT: I have tried adding master = getcanvas() and made that the master of one of the widgets like so:

master = getcanvas()
mb = Menubutton(master, text = "Change color")

And now when I run the program, it just hangs and eventually crashes every single time. I had tried this before when I had only one widget, and it still yielded the same results. I don't really know what else to do, so help is very much appreciated.

EDIT # 2: I have also tried to create a tkinter frame that I pack to the bottom of the canvas and then make that frame the master of the menu, like so:

master = Frame()
master.pack(side = 'top')
mb = Menubutton(master, text = "Change color")
mb.pack(side = 'top')

However, even though both are set to side = 'top' , it still stays at the bottom of the screen, with just the widget on top of the others, like so:

新方法失败

What is still going wrong?

EDIT # 3: This is what I am now getting as per the answers advice:

我现在有什么

Your problem is in the order you are calling the geometry manager pack

pack will add the widget to the available space on the master widget/frame/window, and stick it to the specified edge (default is top ). So in your case you are calling pack on the canvas first, which will stick it to the top of your window, and then whatever you add, will be added under it, because it is sticked to the top edge, and your available space is under.

You have several options to do what you want to do, you can change the order of declaration of your widgets, declaring first what's going to be on top, or left, and pack it as such. Which is not very pretty. Or you can declare all your widgets and leave all the pack commands at the end, and then calling them in the order you need them, which is better but still not the best option.

I would suggest create different frames for each group of elements, for example a frame of menus, a frame of buttons and a frame for the canvas, manage the internal geometry of the elements in each frame, and then manage the geometry of the frames in the window.

Or you can use grid instead of pack to manage the geometry. Mind you it is not recommended to mix the use of both, it can get messy.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM