简体   繁体   中英

tkinter create_window erases previous window

For some reason, when I go to create_window in my Tkinter canvas, it erases everything that was previously in said window, and jams the window in the top left corner (even though I set it somewhere else.

canvas.create_window(30, height - 40, anchor = NW, width = 40,
    window = canvas.data.buildSquareButton)

precedes

canvas.create_rectangle(0,0,width, 40, fill = "#888888888",
                        outline = "#888888888")
canvas.create_rectangle(0, height, width, (height-40), fill = "#888888888",
                        outline = "#888888888")
canvas.create_rectangle(0, 40, width, (height - 40), fill = "#fffffffff",
                        outline = "#fffffffff")

and an image.

I put in a 1 second time.sleep after the create_window, and I could see that the button was put in the right place. Then after the time.sleep was over, the button threw itself in the top right corner and the rectangle never appeared. I commented out the window, and the rectangles appeared fine.

Am I doing something wrong when I call the window, or is there a Tkinter glitch?

There's not enough information in your question to know for sure. However, my guess is that you are pack ing or grid ing a widget in the canvas, and that's causing the canvas to shrink to fit its contents. Or, you're doing something else to cause the canvas to shrink.

To compound the problem, your canvas probably has the same background color as your main window, so you think the contents are being erased, but in reality you're looking at the widget that the canvas is in rather than the canvas itself.

To help prove or disprove that theory, give your canvas a garish background color, such as a bright red. Then run your code and see what happens to the red part of the screen.

Bottom line: there is no bug in tkinter that would cause the behavior you describe. There is a bug in some code that you aren't showing us.

The best thing is for you to create the smallest possible program that reproduces the problem. The mere act of trying to do that may expose the bug in your code. If you are able to reproduce it in a dozen or two lines of code, update the question and we can probably spot the error.

I'm not entirely sure what your question is, but it looks like you're trying to remove everything from a canvas widget at one point in your code to allow for other things to override it, yes?

Try this to reset the canvas: canvas.delete("all")

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