Python 如何向 tkinter 窗口添加边距?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3643235/
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
How to add a margin to a tkinter window?
提问by Hyman S.
So I have so far a simple python tkinter window and i'm adding text, buttons, etc.
所以到目前为止我有一个简单的 python tkinter 窗口,我正在添加文本、按钮等。
snippet:
片段:
class Cfrm(Frame):
def createWidgets(self):
self.text = Text(self, width=50, height=10)
self.text.insert('1.0', 'some text will be here')
self.text.tag_configure('big', font=('Verdana', 24, 'bold'))
self.text["state"] = "disabled"
self.text.grid(row=0, column=1)
self.quitw = Button(self)
self.quitw["text"] = "exit",
self.quitw["command"] = self.quit
self.quitw.grid(row=1, column=1)
def __init__(self, master=None):
Frame.__init__(self, master)
self.pack()
self.createWidgets()
the problem is, I want to have about a 15-20 px margin around the window, I looked everywhere, and I couldn't find a solution. Also
问题是,我想在窗口周围留出大约 15-20 像素的边距,我四处寻找,但找不到解决方案。还
self.text.tag_configure('big', font=('Verdana', 24, 'bold'))
doesn't work. Any possible solutions?
不起作用。任何可能的解决方案?
采纳答案by Hyman S.
Ok, here is the solution I found for question 1:
好的,这是我为问题 1 找到的解决方案:
self.grid(padx=20, pady=20)
Removing .textseems to change the whole frame. I still haven't solved problem 2.
删除.text似乎改变了整个框架。我还没有解决问题2。
回答by Bryan Oakley
Use the pad options (padx, pady, ipadx, ipady) for the grid command to add padding around the text widget. For example:
使用网格命令的填充选项(padx、pady、ipadx、ipady)在文本小部件周围添加填充。例如:
self.text.grid(row=0, column=1, padx=20, pady=20)
If you want padding around the whole GUI, add padding when you pack the application frame:
如果要在整个 GUI 周围进行填充,请在打包应用程序框架时添加填充:
self.pack(padx=20, pady=20)
When you say the tag command doesn't work, how do you define "doesn't work"? Are you getting an error? Does the font look big but not bold, bold but not big, ...? The command looks fine to me, and when I run it it works fine.
当您说 tag 命令不起作用时,您如何定义“不起作用”?你有错误吗?字体看起来大但不粗,粗但不大,......?该命令对我来说看起来不错,当我运行它时它工作正常。
Your example doesn't show that you're actually applying that tag to a range of text. Are you? If so, how? If you do the following, what happens?
您的示例并未表明您实际上将该标签应用于一系列文本。你是?如果是这样,如何?如果您执行以下操作,会发生什么?
self.text.insert("1.0", 'is this bold?', 'big')
回答by rectangletangle
A quick way to do it is adjust your relief style to flat, then you only have to adjust your border width.
一个快速的方法是将您的浮雕样式调整为平面,然后您只需调整边框宽度。
self.Border = Tkinter.Frame(self, relief='flat', borderwidth=4)

