更新时间:2022-12-20 13:20:57
你说你不能使用 grid_propagate(False)
是错的,因为你可以.grid_propagate
与实际大小有关,与属性无关.此外,如果您只是使用 wm_geometry
为您的应用程序提供固定大小,则字体更改不会影响窗口的大小.
You are wrong when you say you can't use grid_propagate(False)
, because you can. grid_propagate
is related to the actual size, not the size attribute. Also, if you simply give your application a fixed size using wm_geometry
, font changes won't affect the size of the window.
以下是使用 grid_propagate
的示例,它将容器设置为以像素为单位的固定大小:
Here's an example using grid_propagate
, which sets the container to a fixed size in pixels:
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()