[英]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.