Tkinter Entry框的字体未随默认设置更改。

huangapple go评论70阅读模式
英文:

Tkinter Entry Box Font not changing with default

问题

我有一个正在构建的应用程序的GUI,我想让用户有选择地更新字体样式和大小。以下是我迄今为止所做的工作,这可以更改按钮和标签上的字体,但它不会更改任何Entry()小部件的字体,而这可能是我希望用户能够更改的最重要的文本。我知道我可以通过提供类似这样的内容来初始化Entry()小部件字体:font=('Arial Bold', 16),这确实有效,但它不允许用户有任何控制。

我确实发现您可以使用.config(font=('Arial Bold', 16))更改特定小部件。所以从理论上讲,我可以在我的apply_font()函数中为每个Entry()小部件包含这个,但那不可能是唯一的方法,对吧?我想应该有一种方法可以全局编辑所有Entry()小部件使用的字体。

我该如何完成这个任务?

def call_font_window():
    font_window = tk.Toplevel()
    font_window.title('Fonts')
    font_window.resizable(False, False)
    font_window.geometry('350x150')
    font_window.grid_columnconfigure(1, weight=1)

    font_type_label = tk.Label(font_window, text='Font Type: ')
    font_size_label = tk.Label(font_window, text='Font Size: ')

    font_type_options = [def_family, 'Calibri', 'Arial', 'Arial Bold', 'Times New Roman']
    font_size_options = [def_size, 10, 12, 14, 16]

    font_type_entry = tk.OptionMenu(font_window, font_type_var, *font_type_options)
    font_size_entry = tk.OptionMenu(font_window, font_size_var, *font_size_options)

    font_type_label.grid(row=0, column=0, sticky='NEW')
    font_size_label.grid(row=1, column=0, sticky='NEW')
    font_type_entry.grid(row=0, column=1, sticky='NEW')
    font_size_entry.grid(row=1, column=1, sticky='NEW')

    def apply_font():
        def_font.configure(family=font_type_var.get(), size=font_size_var.get())

    apply_button = tk.Button(font_window, text='Apply', command=apply_font)
    apply_button.grid(row=2, column=1, sticky='NEW')

    font_window.grab_set()
    font_window.mainloop()

# 在这里放置其余的代码...

希望这可以帮助你全局更改所有Entry()小部件的字体。

英文:

I have a GUI for an application I am building, and I want to give users the option to update the font style and size. Below is what I have done so far and this works for changing the font on buttons & labels, but it doesn't change the font of any of the Entry() widgets, which is probably the most important text I want the user to be able to change. I know I can initialize the Entry() widget font by providing something like this font=('Arial Bold', 16) which works, but it doesn't allow the user any control.

I did figure out that you can change specific widgets using .config(font=('Arial Bold', 16)). So theoretically, I could include this in my apply_font() function for every Entry() widget, but that can't be the only way, right? I imagine there has to be a way to globally edit the font all Entry() widgets use.

How can I accomplish this?

def call_font_window():
    font_window = tk.Toplevel()
    font_window.title('Fonts')
    font_window.resizable(False, False)
    font_window.geometry('350x150')
    font_window.grid_columnconfigure(1, weight=1)

    font_type_label = tk.Label(font_window, text='Font Type: ')
    font_size_label = tk.Label(font_window, text='Font Size: ')  

    font_type_options = [def_family, 'Calibri', 'Arial', 'Arial Bold', 'Times New Roman']
    font_size_options = [def_size, 10, 12, 14, 16]

    font_type_entry = tk.OptionMenu(font_window, font_type_var, *font_type_options)
    font_size_entry = tk.OptionMenu(font_window, font_size_var, *font_size_options)

    font_type_label.grid(row=0, column=0, sticky='NEW')
    font_size_label.grid(row=1, column=0, sticky='NEW')
    font_type_entry.grid(row=0, column=1, sticky='NEW')
    font_size_entry.grid(row=1, column=1, sticky='NEW')

    def apply_font():
        def_font.configure(family=font_type_var.get(), size=font_size_var.get())


    apply_button = tk.Button(font_window, text='Apply', command=apply_font)
    apply_button.grid(row=2, column=1, sticky='NEW')

    font_window.grab_set()
    font_window.mainloop()

.
.
.

def_font = font.nametofont("TkDefaultFont")
def_family = copy.deepcopy(def_font['family'])
def_size = copy.deepcopy(def_font['size'])
font_type_var = tk.StringVar(value=def_font['family'])
font_size_var = tk.IntVar(value=def_font['size'])

menubar = tk.Menu(root)
view_options = tk.Menu(menubar, tearoff=0) 
view_options.add_command(label="Font", command=call_font_window)
view_options.add_separator() 
menubar.add_cascade(label="View", menu=view_options)
root.config(menu=menubar)
.
.
.

答案1

得分: 2

所有小部件的字体未由TkDefaultFont字体指定。转到TkDocs教程-字体 标准字体部分以获取更多信息。

最好将apply_font函数更改为以下内容:

def apply_font():
    font_ = font.nametofont("TkDefaultFont")
    font_.configure(family=font_type_var.get(), size=font_size_var.get())
    font_ = font.nametofont("TkTextFont")
    font_.configure(family=font_type_var.get(), size=font_size_var.get())
英文:

All the widgets fonts do not specifies by TkDefaultFont font. Go to TkDocs Tutorial - Fonts Standard Fonts section for more information.

You had better chage apply_font function as follow:

def apply_font():
    font_ = font.nametofont("TkDefaultFont")
    font_.configure(family=font_type_var.get(), size=font_size_var.get())
    font_ = font.nametofont("TkTextFont")
    font_.configure(family=font_type_var.get(), size=font_size_var.get())

答案2

得分: 0

我找到了一个解决方案,尽管我认为这不是最佳方法,所以如果有其他方法,请提供替代方案。

我导入了gc(垃圾回收器),并将以下内容添加到我的apply_font函数中:

for obj in gc.get_referrers(tk.Entry):
    if obj.__class__ is tk.Entry:
        obj.config(font=(font_type_var.get(), font_size_var.get()))

目前我认为这是一个胜利,因为它可以做我想做的事情。

英文:

I figured out a solution, though I don't think it is the best way to go about this so please provide alternatives if they are available.

I imported gc (garbage collector) and added the following to my apply_font function:

for obj in gc.get_referrers(tk.Entry):
            if obj.__class__ is tk.Entry:
                obj.config(font=(font_type_var.get(), font_size_var.get()))

I'm calling this a win for now as it does the thing I'd like it to do.

huangapple
  • 本文由 发表于 2023年6月26日 21:21:22
  • 转载请务必保留本文链接:https://go.coder-hub.com/76557103.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定