How to refresh a tkinter text box?

Question:

I made a script, a small function which adds 1 to the int x and used it in the text box as the size. The problem is that the text box doesn’t read the new size of the font after i call the function so how do i refresh the text box for it to use the latest x value?

import re
from tkinter import *
from tkinter import ttk
import time

root = Tk()
root.title('Notepad')
root.iconbitmap('C:/Users/Hero/Documents/Visual Studio code/My project/notes.ico')
root.geometry("500x500")

root.tk.call("source", "C:/Users/Hero/Documents/Visual Studio code/My project/azure.tcl")
root.tk.call("set_theme", "dark")

x = 20

def change_theme():
    if root.tk.call("ttk::style", "theme", "use") == "azure-dark":
        root.tk.call("set_theme", "light")
    else:
        root.tk.call("set_theme", "dark")

def font_size():
    global x
    x = x + 1
    print(x)
    text=Text(root, font=("Georgia", x), yscrollcommand=scroll.set, bg='#292929', height=1, width=1)
    

style=ttk.Style()
style.theme_use('azure-dark')
style.configure("Vertical.TScrollbar", background="grey", bordercolor="black", arrowcolor="white")

barframe = Frame()
barframe.pack(side=BOTTOM)

fonts = ttk.Button(barframe, text='Size and Font', width=15, style='Accent.TButton', command=font_size)
calculater = ttk.Button(root, text='Calculater', width=15, style='Accent.TButton')
fonts.grid(row=0, column=0)

scroll = ttk.Scrollbar(root, orient='vertical')
scroll.pack(side=RIGHT, fill='y')

text=Text(root, font=("Georgia", x), yscrollcommand=scroll.set, bg='#292929', height=1, width=1)
scroll.config(command=text.yview)
text.pack(fill=BOTH, expand=1)

root.mainloop()
Asked By: Real Swat

||

Answers:

for change parameter must be used method config or itemconfig for more detail

label=Label(text="hello world")

label.config(text="hello")

#in the same way
text=Text(root, font=("Georgia", x), yscrollcommand=scroll.set, bg='#292929', height=1, width=1)
text.pack(fill=BOTH, expand=1)
text.config(font=("Georgia", x))

Answered By: h sha

Instead of text = Text(...) in your font_size function, which creates a new Text, you want to update the one that already exists.

text.config(font=("Georgia", x))
Answered By: Filip Müller
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.