当你说无法使用grid_propagate(False)
时是错误的,因为你是可以使用它的。 grid_propagate
与实际大小有关,而不是大小属性。 此外,如果你使用wm_geometry
简单地给你的应用程序设定了一个固定大小,字体更改也不会影响窗口的大小。
以下是使用grid_propagate
的示例,它将容器设置为像素固定大小:
import Tkinter as tk
import tkFont
class SampleApp(tk.Tk):
def __init__(self, *args, **kwargs):
tk.Tk.__init__(self, *args, **kwargs)
self._textFont = tkFont.Font(name="TextFont")
self._textFont.configure(**tkFont.nametofont("TkDefaultFont").configure())
toolbar = tk.Frame(self, borderwidth=0)
container = tk.Frame(self, borderwidth=1, relief="sunken",
width=600, height=600)
container.grid_propagate(False)
toolbar.pack(side="top", fill="x")
container.pack(side="bottom", fill="both", expand=True)
container.grid_rowconfigure(0, weight=1)
container.grid_columnconfigure(0, weight=1)
text = tk.Text(container, font="TextFont")
text.grid(row=0, column=0, sticky="nsew")
zoomin = tk.Button(toolbar, text="+", command=self.zoom_in)
zoomout = tk.Button(toolbar, text="-", command=self.zoom_out)
zoomin.pack(side="left")
zoomout.pack(side="left")
text.insert("end", '''Press te + and - buttons to increase or decrease the font size''')
def zoom_in(self):
font = tkFont.nametofont("TextFont")
size = font.actual()["size"]+2
font.configure(size=size)
def zoom_out(self):
font = tkFont.nametofont("TextFont")
size = font.actual()["size"]-2
font.configure(size=max(size, 8))
if __name__ == "__main__":
app = SampleApp()
app.mainloop()
grid_propagate
的说法是错误的。它与大小属性无关,而与小部件的实际大小有关。如果您可以展示使用grid_propagate
并且它不起作用的代码,我们可以修复它。听起来你不理解grid_propagate
的工作原理。你知道你必须在父级上调用它吗?在文本小部件本身上调用grid_propagate
没有任何效果。 - Bryan Oakley