简体   繁体   中英

problem with font size in following python tkinter code whenever i run my program font size do not increase

why font size is not increasing although i have clearly mentioned it in my following code please someone check tell me the problem. whenever i my code and select on text size and click on some number like 8 0r 9 or any other number font size should also change according to number like in small number font size should be small and in big number font size should be big but font size is not changing.

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()

Your code has a fatal flaw. Each time you modify the font you give the widget an entirely new font. For example, consider these two lines:

editor.config(font = 'Terminal')
editor.config(font = 13)

Each time you call one of those statements, you aren't changing just one property, you are configuring an entirely new font. When you call editor.config(font=13) you're telling tkinter to create an entirely new font named "13", and it will use all of tkinter's defaults for size, family, etc.

Once you go from wanting a single font to a configurable font, creating a new font every time you want to change an attribute is the wrong solution. Instead, you should be using font objects.

When you use a font object and you change the size of that font object, all widgets that use that font will automatically change. This is a very powerful but highly under-utilized feature of tkinter.

Let's start with a tiny working program:

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()

Notice that we created a custom font named textFont and initialized the size to be 18 points. We then created a text widget that uses this font. We could also set any of the other parameters in a similar fashion (family, weight, slant, underline, overstrike), but I left that out for the sake of simplicity.

We can modify the font used by the text widget by changing the textFont object. For example, here's a function that will cause the font to grow or shrink by whatever amount we want. When called, every widget which uses the same textFont will grow or shrink.

def change_font_size(delta):
    current_size = textFont.cget("size")
    new_size = int(current_size) + delta
    textFont.configure(size=new_size)

For example, to make the font two points larger you would call it like this:

change_font_size(2)

Let's add a simple "View" menu to let us grow and shrink the font:

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))

Now, whenever you select "Zoom In" or "Zoom Out", the function will be called, the font will change, and any widgets which use the font will automatically be updated.

You can change the font family in a similar fashion. It might look something like this:

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))

----

For information about how to modify tkinter's default font without creating a new font, see this answer to the question Modify the default font in Python Tkinter

Try something like this:

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()

It uses 2 global variables: font_family and font_size for the font and updates the font whenever any button is pressed

why font size is not increasing although i have clearly mentioned it in my following code please someone check tell me the problem. whenever i my code and select on text size and click on some number like 8 0r 9 or any other number font size should also change according to number like in small number font size should be small and in big number font size should be big but font size is not changing.

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()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM