如何解决每当我运行我的程序时,以下 python tkinter 代码中的字体大小问题不会增加
为什么字体大小没有增加,尽管我在下面的代码中明确提到了它,请有人检查告诉我问题。每当我我的代码并选择文本大小并单击一些数字时,例如 8 0r 9 或任何其他数字字体大小也应根据数字更改,如小数字字体大小应小,大数字字体大小应大但字体尺寸没有变化。
from tkinter import *
import tkinter.font as font
root = Tk()
root.title("MyCodeEditor")
def run():
code = editor.get('1.0',END)
exec(code)
############################ colour theme ######################################################3#
def dark():
editor.config(background = "black",foreground = "white",insertbackground = "white")
root.config(background = "black")
def light():
editor.config(background = "white",foreground = "black",insertbackground = "black" )
root.config(background = "white")
def blue_sky():
editor.config(background = "powder blue",insertbackground = "black" )
root.config(background = "powder blue")
####################################### Text style ####################################################33
def terminal_family():
editor.config(font = 'Terminal')
def modern_family():
editor.config(font = ('Modern'))
def roman_family():
editor.config(font = 'Roman')
def default_family():
editor.config(font = 'Calibri')
def system_family():
editor.config(font = 'System')
def aerial_family():
editor.config(font = 'Aerial')
def ms_serif_family ():
editor.config(font = 'MS_Serif')
############################ Text size #################################################################################################
def eight_size():
editor.config(font = 8)
def nine_size():
editor.config(font = 9)
def ten_size():
editor.config(font = 10)
def eleven_size():
editor.config(font = 11)
def twelve_size():
editor.config(font = 12)
def thirteen_size():
editor.config(font = 13)
def fourteen_size():
editor.config(font = 14)
def fifteen_size():
editor.config(font = 15)
def sixteen_size():
editor.config(font = 16)
def seventeen_size():
editor.config(font = 17)
def eighteen_size():
editor.config(font = 18)
def nineteen_size():
editor.config(font = 19)
def twenty_size():
editor.config(font = 20)
def twenty_five_size():
editor.config(font = 25)
def thirty_size():
editor.config(font = 30)
# menu_bar = Menu(root)
####################################### Menu ############################################################
menu_bar = Menu(root)
file_bar = Menu(menu_bar,tearoff = 0)
file_bar.add_command(label = 'Open',command = run )
file_bar.add_command(label = 'Save',command = run )
file_bar.add_command(label = 'Save as',command = run )
file_bar.add_command(label = 'Exit',command = exit )
menu_bar.add_cascade(label='File',menu = file_bar)
root.config(menu = menu_bar)
###########################################################################################################
run_bar = Menu(menu_bar,tearoff = 0)
run_bar.add_command(label = 'Run',command = run )
menu_bar.add_cascade(label='Run',menu = run_bar)
root.config(menu = menu_bar)
##############################################################################################################
colorTheme_bar = Menu(menu_bar,tearoff = 0)
colorTheme_bar.add_command(label = 'Light Mode',command = light )
colorTheme_bar.add_command(label = 'Dark Mode',command = dark )
colorTheme_bar.add_command(label = 'Blue Sky Mode',command = blue_sky )
menu_bar.add_cascade(label='Color Theme',menu = colorTheme_bar)
root.config(menu = menu_bar)
# file_bar = Menu(menu_bar,tearoff = 0)
# file_bar.add_command(label = 'Open',command = run )
# menu_bar.add_cascade(label='File',menu = file_bar)
# root.config(menu = menu_bar)
#####################################################################################################################
fontStyle_bar = Menu(menu_bar,tearoff = 0)
fontStyle_bar.add_command(label = 'Default',command = default_family)
fontStyle_bar.add_command(label = 'Modern',command = modern_family )
fontStyle_bar.add_command(label = 'Roman',command = roman_family )
fontStyle_bar.add_command(label = 'Ms serif',command = ms_serif_family )
fontStyle_bar.add_command(label = 'Aerial',command = aerial_family )
fontStyle_bar.add_command(label = 'System',command = system_family )
fontStyle_bar.add_command(label = 'Terminal',command = terminal_family )
menu_bar.add_cascade(label='Text Style',menu = fontStyle_bar)
root.config(menu = menu_bar)
######################################################################################################################################
textSize_bar = Menu(menu_bar,tearoff = 0)
textSize_bar.add_command(label = 8,command = eight_size )
textSize_bar.add_command(label = 9,command = nine_size )
textSize_bar.add_command(label = 10,command = ten_size )
textSize_bar.add_command(label = 11,command = eleven_size )
textSize_bar.add_command(label = 12,command = twelve_size )
textSize_bar.add_command(label = 13,command = thirteen_size )
textSize_bar.add_command(label = 14,command = fourteen_size )
textSize_bar.add_command(label = 15,command = fifteen_size )
textSize_bar.add_command(label = 16,command = sixteen_size )
textSize_bar.add_command(label = 17,command = seventeen_size )
textSize_bar.add_command(label = 18,command = eighteen_size )
textSize_bar.add_command(label = 19,command = nineteen_size )
textSize_bar.add_command(label = 20,command = twenty_size )
textSize_bar.add_command(label = 25,command = twenty_five_size )
textSize_bar.add_command(label = 30,command = thirty_size )
menu_bar.add_cascade(label='Text Size',menu = textSize_bar)
root.config(menu = menu_bar)
#########################################################################################################
editor = Text()
editor.pack()
root.mainloop()
解决方法
你的代码有一个致命的缺陷。每次修改字体时,都会为小部件提供一种全新的字体。例如,考虑以下两行:
editor.config(font = 'Terminal')
editor.config(font = 13)
每次调用其中一个语句时,您不仅仅是在更改一个属性,而是在配置一种全新的字体。当您调用 editor.config(font=13)
时,您是在告诉 tkinter 创建一个名为“13”的全新字体,它将使用 tkinter 的所有默认字体大小、系列等。
一旦您从需要单一字体变为可配置字体,每次您想更改属性时都创建新字体是错误的解决方案。相反,您应该使用字体对象。
当您使用字体对象并更改该字体对象的大小时,所有使用该字体的小部件都会自动更改。这是 tkinter 的一个非常强大但未被充分利用的功能。
让我们从一个小程序开始:
import tkinter as tk
from tkinter.font import Font
root = tk.Tk()
root.geometry("600x600")
textFont = Font(size=18)
text = tk.Text(root,font=textFont)
text.pack(fill="both",expand=True)
root.mainloop()
请注意,我们创建了一个名为 textFont
的自定义字体并将大小初始化为 18 磅。然后我们创建了一个使用这种字体的文本小部件。我们还可以以类似的方式设置任何其他参数(族、重量、倾斜、下划线、重叠),但为了简单起见,我将其省略了。
我们可以通过更改 textFont
对象来修改文本小部件使用的字体。例如,这里有一个函数可以使字体按我们想要的任意数量增大或缩小。调用时,每个使用相同 textFont
的小部件都会增大或缩小。
def change_font_size(delta):
current_size = textFont.cget("size")
new_size = int(current_size) + delta
textFont.configure(size=new_size)
例如,要使字体大两点,您可以这样称呼它:
change_font_size(2)
让我们添加一个简单的“查看”菜单,让我们放大和缩小字体:
menubar = tk.Menu(root)
root.configure(menu=menubar)
viewMenu = tk.Menu(menubar)
menubar.add_cascade(label="View",menu=viewMenu)
viewMenu.add_command(label="Zoom In",command=lambda: change_font_size(2))
viewMenu.add_command(label="Zoom Out",command=lambda: change_font_size(-2))
现在,每当您选择“放大”或“缩小”时,都会调用该函数,更改字体,并且任何使用该字体的小部件都会自动更新。
您可以以类似的方式更改字体系列。它可能看起来像这样:
def change_font_family(family):
textFont.configure(family=family)
...
fontFamilyMenu = tk.Menu(...)
for family in ("Terminal","Ariel",...):
fontFamilyMenu.add_command(label=family,command=lambda family=family: change_font_family(family))
----
有关如何在不创建新字体的情况下修改 tkinter 的默认字体的信息,请参阅问题 this answer
的 Modify the default font in Python Tkinter ,尝试这样的事情:
import tkinter as tk
def change_size(new_size):
global font_size
font_size = new_size
label.config(font=(font_family,font_size))
def change_font(new_font):
global font_family
font_family = new_font
label.config(font=(font_family,font_size))
font_family = ""
font_size = 10
root = tk.Tk()
label = tk.Label(root,text="This is the label.",font=(font_family,font_size))
label.pack()
for i in range(8,26):
text = f"Change text's size to: {i}"
button = tk.Button(root,text=text,command=lambda i=i: change_size(i))
button.pack()
font_families_available = ("Terminal","Modern","Roman")
for font_family_name in font_families_available:
text = f"Change text's font to: {font_family_name}"
command = lambda font_family_name=font_family_name: change_font(font_family_name)
button = tk.Button(root,command=command)
button.pack()
root.mainloop()
它使用 2 个全局变量:font_family
和 font_size
作为字体并在按下任何按钮时更新字体
变化如下:
将所有选择字体大小的函数更新为此。如果你不想改变你的字体,你可以这样做:font=("",<size>)
def eight_size():
editor.config(font = ("arial",8))
def nine_size():
editor.config(font = ("arial",9))
def ten_size():
editor.config(font = ("arial",10))
def eleven_size():
editor.config(font = ("arial",11))
def twelve_size():
editor.config(font = ("arial",12))
def thirteen_size():
editor.config(font = ("arial",13))
def fourteen_size():
editor.config(font = ("arial",14))
def fifteen_size():
editor.config(font = ("arial",15))
def sixteen_size():
editor.config(font = ("arial",16))
def seventeen_size():
editor.config(font = ("arial",17))
def eighteen_size():
editor.config(font = ("arial",18))
def nineteen_size():
editor.config(font = ("arial",19))
def twenty_size():
editor.config(font = ("arial",20))
def twenty_five_size():
editor.config(font = ("arial",25))
def thirty_size():
editor.config(font = ("arial",30))
替换选择字体的菜单:
textSize_bar = Menu(menu_bar,tearoff = 0)
textSize_bar.add_command(label = 8,command = eight_size )
textSize_bar.add_command(label = 9,command = nine_size )
textSize_bar.add_command(label = 10,command = ten_size )
textSize_bar.add_command(label = 11,command = eleven_size )
textSize_bar.add_command(label = 12,command = twelve_size )
textSize_bar.add_command(label = 13,command = thirteen_size )
textSize_bar.add_command(label = 14,command = fourteen_size )
textSize_bar.add_command(label = 15,command = fifteen_size )
textSize_bar.add_command(label = 16,command = sixteen_size )
textSize_bar.add_command(label = 17,command = seventeen_size )
textSize_bar.add_command(label = 18,command = eighteen_size )
textSize_bar.add_command(label = 19,command = nineteen_size )
textSize_bar.add_command(label = 20,command = twenty_size )
textSize_bar.add_command(label = 25,command = twenty_five_size )
textSize_bar.add_command(label = 30,command = thirty_size )
menu_bar.add_cascade(label='Text Size',menu = textSize_bar)
root.config(menu = menu_bar)
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。