Python tkinter 按钮的高度和宽度
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20004689/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
tkinter button height and width
提问by user2996828
I am trying to create a button and change the height and width using the code below but the actual button doesn't show physically. However if you hover over the area it is supposed to be and click it will open the new window. Any ideas?
我正在尝试使用下面的代码创建一个按钮并更改高度和宽度,但实际按钮并未实际显示。但是,如果您将鼠标悬停在它应该位于的区域上并单击它,则会打开新窗口。有任何想法吗?
import sys
from tkinter import *
#main menu
def mmWindow():
mmWindow=Tk()
mmWindow.geometry('600x600')
#first window
mWindow= Tk()
mWindow.geometry('1920x1080+0+0')
mWindow.title('DMX512 Controller')
wtitle = Label (mWindow, text = "Pi DMX", fg = 'blue')
wtitle.place(x = 640, y = 100)
#main menu button
mmbutton = Button (mWindow, text = "Main Menu",command = mmWindow)
mmbutton.place( x=200, y = 200)
mmbutton.config(width=200, height=200)
回答by user2996828
Regarding your initial question: the button doesappear physically. The problem is, since it is so large, it is hard to distinguish from the rest of the window.
关于您最初的问题:按钮确实出现在物理上。问题是,由于它太大,很难与窗口的其余部分区分开来。
Now, you said that your ultimate goal is to change the size of a button. If so, then you are on the right track: you use the heightand widthoptions for this.
现在,您说过您的最终目标是更改按钮的大小。如果是这样,那么您就在正确的轨道上:您可以使用height和width选项。
However, I would recommend that you make a few changes to your code:
但是,我建议您对代码进行一些更改:
- Don't make the button so huge. Even on a very big monitor, having a button be that size is way overkill.
- Don't make the window so huge. Nobody wants an application that takes up the entire screen.
- Use
.gridinstead of.place. Doing so will make it easier for you to place widgets where you want them. - Set the
heightandwidthoptions when you make the button, not after it. - There is no need to import
syshere. Only import what you need. - Don't import like this:
from tkinter import *. Doing so dumps a whole bunch of names in the global namespace that can easily be overwritten.
- 不要把按钮弄得那么大。即使在一个非常大的显示器上,有一个这么大的按钮也太过分了。
- 不要把窗户弄得那么大。没有人想要占据整个屏幕的应用程序。
- 使用
.grid代替.place。这样做将使您更轻松地将小部件放置在您想要的位置。 - 在制作按钮时设置
height和width选项,而不是在按钮之后。 - 这里不需要导入
sys。只导入你需要的东西。 - 不要像这样导入:
from tkinter import *. 这样做会在全局命名空间中转储一大堆名称,这些名称很容易被覆盖。
Here is my version of your script:
这是我的脚本版本:
import tkinter as tk
def mmWindow():
mmWindow = tk.Tk()
mmWindow.geometry('600x600')
mWindow = tk.Tk()
# You can set any size you want
mWindow.geometry('500x500+0+0')
mWindow.title('DMX512 Controller')
wtitle = tk.Label(mWindow, text="Pi DMX", fg='blue')
wtitle.grid(row=0, column=1)
# You can set any height and width you want
mmbutton = tk.Button(mWindow, height=5, width=20, text="Main Menu", command=mmWindow)
mmbutton.grid(row=1, column=1)
mWindow.mainloop()

