如何刷新文本框?



>我做了一个脚本,一个小函数,它把 1 加到 intx,并在文本框中使用它作为大小。问题是在我调用函数后文本框没有读取字体的新大小,那么如何刷新文本框以使其使用最新的x值?

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

而不是在font_size函数中text = Text(...),这会创建一个新Text,而是要更新已经存在的函数。

text.config(font=("Georgia", x))

对于更改参数,必须使用方法configitemconfig以获取更多详细信息

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

最新更新