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

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

Tkinter Entry Box Font not changing with default

问题

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

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

我该如何完成这个任务?

  1. def call_font_window():
  2. font_window = tk.Toplevel()
  3. font_window.title('Fonts')
  4. font_window.resizable(False, False)
  5. font_window.geometry('350x150')
  6. font_window.grid_columnconfigure(1, weight=1)
  7. font_type_label = tk.Label(font_window, text='Font Type: ')
  8. font_size_label = tk.Label(font_window, text='Font Size: ')
  9. font_type_options = [def_family, 'Calibri', 'Arial', 'Arial Bold', 'Times New Roman']
  10. font_size_options = [def_size, 10, 12, 14, 16]
  11. font_type_entry = tk.OptionMenu(font_window, font_type_var, *font_type_options)
  12. font_size_entry = tk.OptionMenu(font_window, font_size_var, *font_size_options)
  13. font_type_label.grid(row=0, column=0, sticky='NEW')
  14. font_size_label.grid(row=1, column=0, sticky='NEW')
  15. font_type_entry.grid(row=0, column=1, sticky='NEW')
  16. font_size_entry.grid(row=1, column=1, sticky='NEW')
  17. def apply_font():
  18. def_font.configure(family=font_type_var.get(), size=font_size_var.get())
  19. apply_button = tk.Button(font_window, text='Apply', command=apply_font)
  20. apply_button.grid(row=2, column=1, sticky='NEW')
  21. font_window.grab_set()
  22. font_window.mainloop()
  23. # 在这里放置其余的代码...

希望这可以帮助你全局更改所有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?

  1. def call_font_window():
  2. font_window = tk.Toplevel()
  3. font_window.title('Fonts')
  4. font_window.resizable(False, False)
  5. font_window.geometry('350x150')
  6. font_window.grid_columnconfigure(1, weight=1)
  7. font_type_label = tk.Label(font_window, text='Font Type: ')
  8. font_size_label = tk.Label(font_window, text='Font Size: ')
  9. font_type_options = [def_family, 'Calibri', 'Arial', 'Arial Bold', 'Times New Roman']
  10. font_size_options = [def_size, 10, 12, 14, 16]
  11. font_type_entry = tk.OptionMenu(font_window, font_type_var, *font_type_options)
  12. font_size_entry = tk.OptionMenu(font_window, font_size_var, *font_size_options)
  13. font_type_label.grid(row=0, column=0, sticky='NEW')
  14. font_size_label.grid(row=1, column=0, sticky='NEW')
  15. font_type_entry.grid(row=0, column=1, sticky='NEW')
  16. font_size_entry.grid(row=1, column=1, sticky='NEW')
  17. def apply_font():
  18. def_font.configure(family=font_type_var.get(), size=font_size_var.get())
  19. apply_button = tk.Button(font_window, text='Apply', command=apply_font)
  20. apply_button.grid(row=2, column=1, sticky='NEW')
  21. font_window.grab_set()
  22. font_window.mainloop()
  23. .
  24. .
  25. .
  26. def_font = font.nametofont("TkDefaultFont")
  27. def_family = copy.deepcopy(def_font['family'])
  28. def_size = copy.deepcopy(def_font['size'])
  29. font_type_var = tk.StringVar(value=def_font['family'])
  30. font_size_var = tk.IntVar(value=def_font['size'])
  31. menubar = tk.Menu(root)
  32. view_options = tk.Menu(menubar, tearoff=0)
  33. view_options.add_command(label="Font", command=call_font_window)
  34. view_options.add_separator()
  35. menubar.add_cascade(label="View", menu=view_options)
  36. root.config(menu=menubar)
  37. .
  38. .
  39. .

答案1

得分: 2

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

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

  1. def apply_font():
  2. font_ = font.nametofont("TkDefaultFont")
  3. font_.configure(family=font_type_var.get(), size=font_size_var.get())
  4. font_ = font.nametofont("TkTextFont")
  5. 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:

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

答案2

得分: 0

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

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

  1. for obj in gc.get_referrers(tk.Entry):
  2. if obj.__class__ is tk.Entry:
  3. 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:

  1. for obj in gc.get_referrers(tk.Entry):
  2. if obj.__class__ is tk.Entry:
  3. 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:

确定