问题描述
global my_frame
my_frame = Frame(tab2,bg="white")
my_frame.place(relwidth= 0.81,relheight= 0.8,relx=0.1,rely=0.1,)
text_scroll = Scrollbar(my_frame)
text_scroll.pack(side=RIGHT,fill=Y)
文本输入
global textBox
textBox = Text(my_frame,undo=True,font=12,yscrollcommand=text_scroll.set)
textBox.place(relwidth= 0.985,relheight= 1,relx=0,rely=0)
text_scroll.config(command=textBox.yview)
我希望能够选择字体的下拉菜单
clicked = StringVar()
clicked.set(12)
drop = OptionMenu(tab2,clicked,1,2,3,4,5,6,7,8,9,10,11,12,13,14)
drop.pack()
我的问题是我不知道如何根据用户选择的数字来设置文本大小。请帮忙。
解决方法
试试这个:
import tkinter as tk
def callback(*args):
new_font_size = int_var.get()
text_widget.config(font=("",new_font_size))
root = tk.Tk()
text_widget = tk.Text(root,font=("",12))
text_widget.pack()
int_var = tk.IntVar(root)
int_var.trace("w",callback)
drop = tk.OptionMenu(root,int_var,*range(1,15))
drop.pack()
root.mainloop()
我将变量从 tk.StringVar
更改为 tk.IntVar
,因为我们正在处理 int
。 font
参数也应该是这样的元组:("<font family name>",<font size>)
。
我正在跟踪 int_var
值是什么并相应地更新 text_widget
。