[英]problem with font size in following python tkinter code whenever i run my program font size do not increase
为什么字体大小没有增加,尽管我在下面的代码中清楚地提到了它,请有人检查告诉我问题所在。 每当我的代码和 select 的文本大小并单击一些数字,如 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", foreground = "black" , 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 的所有默认字体大小、字体系列等。
一旦您 go 从想要单一字体变为可配置字体,每次您想要更改属性时都创建新字体是错误的解决方案。 相反,您应该使用字体对象。
当您使用字体 object 并更改该字体 object 的大小时,使用该字体的所有小部件将自动更改。 这是 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
object 来修改文本小部件使用的字体。例如,这里有一个 function,它将导致字体增大或缩小我们想要的任何数量。 调用时,每个使用相同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))
现在,无论何时您 select “放大”或“缩小”,function 都会被调用,字体会改变,任何使用该字体的小部件都会自动更新。
您可以用类似的方式更改字体系列。 它可能看起来像这样:
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 的默认字体的信息,请参阅此问题的答案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, text=text, command=command)
button.pack()
root.mainloop()
它使用 2 个全局变量:字体的font_family
和font_size
并在按下任何按钮时更新字体
为什么字体大小没有增加,尽管我在下面的代码中明确提到了它,请有人检查告诉我问题。 每当我的代码和 select 在文本大小上并单击某些数字(例如 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", foreground = "black" , 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()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.