简体   繁体   English

tkinter create_window清除上一个窗口

[英]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. 出于某种原因,当我在Tkinter画布中转到create_window时,它会擦除​​该窗口中以前的所有内容,并将该窗口卡在左上角(即使我将其设置在其他位置)。

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. 我在create_window之后放置了1秒钟的时间。我可以看到按钮放置在正确的位置。 Then after the time.sleep was over, the button threw itself in the top right corner and the rectangle never appeared. 然后,在time.sleep结束后,按钮将自身置于右上角,并且矩形从未出现。 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? 呼叫窗口时我做错什么了吗,还是Tkinter出现故障?

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. 但是,我的猜测是您正在将画布中的小部件packgrid ,这导致画布缩小以适合其内容。 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. 底线:tkinter中没有会导致您描述的行为的错误。 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") 尝试以下操作重置画布: canvas.delete("all")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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