简体   繁体   English

Python Tkinter widget.place方法不起作用

[英]Python Tkinter widget.place method does not work

I have some code like 我有一些像

from tkinter import *
root = Tk()
root.geometry("800x700+0+0")

#---------Backgroud of the main canvas-----`enter code here`-------

backGroundCanvas = Canvas(root,bg = 'white', width = 800, height = 600)
backGroundCanvas.focus_set()
backGroundCanvas.pack()

#--------------------------------------------------

#--------The other widgets out of the canvas--------

scoreLabel = Label(root, text = 'Your score is: 0', bg = 'red')
scoreLabel.place(x = 300, y = 750)
scoreLabel.pack()

root.mainloop()

But no matter how I change the parameter of the place method, the label always in the same place and can not be changed.I do not know why! 但是无论我如何更改place方法的参数,标签始终位于同一位置且不能更改。不知道为什么! Please help me , thanks a lot! 请帮助我,非常感谢!

In TKinter there is three basic Geometry manager : Grid, Pack, Place. 在TKinter中,有三个基本的几何管理器:网格,打包,放置。

You should try to avoid the use of place as often as possible using instead Grid or pack ( I personally consider that only grid is a good manager, but this is only my opinion). 您应该尝试避免使用Grid或pack来尽可能多地使用位置(我个人认为只有Grid是一个好的管理者,但这只是我的观点)。

In your code you are setting a position to your widget using place manager, and then give the control to the pack manager. 在代码中,您将使用位置管理器为小部件设置位置,然后将控件提供给包装管理器。 Just remove the scoreLabel.pack() et voila! 只需删除scoreLabel.pack()等即可!

NB : your application is 700px high, and you are trying to place your red label at 750px from the top, you're widget will be outside of the screen. 注意:您的应用程序高度为700像素,并且您尝试将红色标签放置在距顶部750像素处,因此小部件将位于屏幕之外。

NBB : I strongly recommend the use of grid manager. NBB:我强烈建议使用网格管理器。

from tkinter import *
root = Tk()
root.geometry("800x700+0+0")

#---------Backgroud of the main canvas-----`enter code here`-------

backGroundCanvas = Canvas(root,bg = 'white', width = 800, height = 600)
backGroundCanvas.focus_set()
backGroundCanvas.pack()

#--------------------------------------------------

#--------The other widgets out of the canvas--------

scoreLabel = Label(root, text = 'Your score is: 0', bg = 'red')
scoreLabel.place(x = 300, y = 600)

root.mainloop()

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

相关问题 如何使用 widget.place(x,y) 函数将小部件打包到框架中 - 尝试在几个框架之间切换时? (tkinter 蟒蛇) - how to use the widget.place(x,y) function to pack a widget in a frame - when trying to switch between couple of frames? (tkinter python) 如何使python中的tkinter小部件“ .place()”方法与Java中的“ .setbounds()”方法类似地工作? - How make the tkinter widget “.place()” method in python work similarly to the “.setbounds()” method in Java? python tkinter canvas 提升方法不起作用 - python tkinter canvas lift method does not work 如何在 Tkinter Python 中的 .place() 几何方法中的 Entry Widget 中使用 ipadx 和 ipady - How to Use ipadx and ipady in Entry Widget in .place() Geometry Method in Tkinter Python tkinter 中的 Entry 小部件如何工作? - how does the Entry widget in tkinter work? python tkinter 从右边放置方法 - python tkinter place method from right setMouseCallback 方法是否不适用于 tkinter? - does setMouseCallback method not work with tkinter? 为什么我修改过的 tkinter 小部件没有正确放置? - Why does my altered tkinter widget not place correctly? Python Tkinter文本小部件.get方法错误 - Python Tkinter Text Widget .get method error 有没有更好的方法来管理Python中的Tkinter的位置? - Is there any better method to manage place in Tkinter in Python?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM