rea*_*erd 6 python fonts text resize tkinter
我正在尝试为初学者创建一个简单的文字处理器,以便更好地学习Python.
我正在使用Tkinter Text小部件作为主编辑程序,唯一的问题是高度和宽度是由字符定义的.
这在我更改字体时会产生问题,因为并非所有字体的宽度都相同.
每次更改字体时,"文本"窗口小部件都会重新调整大小,但从技术上讲,它的宽度和高度相同.当尝试输入某些东西时,这看起来很荒谬,我试图让文字处理器尽可能好.
有没有办法以像素为单位定义宽度和高度?
的.grid_propagate(False)是没有用的尺寸在技术上是不改变,只有字符宽度.
我正试图远离wxPython现在,因为我到目前为止所做的一切都在Tkinter.
我已经做了无数小时的广泛谷歌搜索,但没有找到解决方案.
Bry*_*ley 10
当你说你不能使用时,你错了grid_propagate(False),因为你可以.grid_propagate与实际大小有关,而与size 属性无关.此外,如果您只是使用固定大小的应用程序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()
Run Code Online (Sandbox Code Playgroud)