When I try to change my global variable from a function in my script nothing is happening?

Viewed 58

This is my Notepad app and in the ChangeSize() function I am trying to change the size of the font and the function is activated on the press of the Label Size in the bottom, but its not doing anything!

Can someone Tell me why the ChangeSize() function is not working?

from tkinter import Tk, scrolledtext, Menu, filedialog, END, messagebox, simpledialog
import os

root = Tk(className=" TEXT EDITOR")
root.title("Untitled.txt - TEXT EDITOR")

F_size = 20
F_family = "Ubuntu Mono"
F_weight = "bold"
F_font = (F_family, F_size, F_weight)

textArea = scrolledtext.ScrolledText(root, width=80, height=30, font=F_font)

#Functions
def ChangeSize():

    global F_size
    F_size = 50

# Menu Options
helpMenu = Menu(menu)

menu.add_cascade(label="Text Options", menu=helpMenu)

helpMenu.add_command(label="Size", command=ChangeSize)
1 Answers

All you're doing is changing a variable. You need to change either the widget or a font object used by widgets.

Tkinter has a very powerful mechanism for managing fonts. You can create font objects and associate them with widgets. Then, whenever you change the font object, all widgets that use that font will automatically adapt to the change.

For example:

from tkinter import font
...
f_font = font.Font(size=20, family="Ubuntu Mono", weight="bold")
textArea = scrolledtext.ScrolledText(..., font=f_font)
...
def ChangeSize():
    f_font.configure(size=50)
Related