我正在尝试创建一个程序,允许用户更改窗口中标签的字体。我无法确定如何使用新字体重新创建所有标签。以下是我到目前为止的情况:
import tkinter as tk
from tkinter import messagebox, colorchooser, simpledialog, filedialog, font
root = tk.Tk()
root.withdraw()
screenHieght = root.winfo_screenheight() # height of the screen
screenWidth = root.winfo_screenwidth() # width of the screen
windowHieght = screenHieght / 2 # Turtle window is half of screen size
windowWidth = screenWidth / 2
x = (screenWidth / 2) - (windowWidth / 2)
y = (screenHieght / 2) - (windowHieght / 2)
root.geometry('%dx%d+%d+%d' % (windowWidth, windowHieght, x, y)) # Some assistance from Google on centering the window
root.columnconfigure(3, weight=2)
root.columnconfigure(1, weight=1)
root.columnconfigure(5, weight=1)
root.rowconfigure(1, weight=2)
root.rowconfigure(3, weight=1)
root.rowconfigure(4, weight=1)
root.rowconfigure(5, weight=1)
root.configure(background="#a1dbcd")
def changeFont(selection):
global selectedfont
global setfontstyle
print(selection)
selectedfont = selection
print(setfontstyle)
def changeSize(val):
print(val)
global setfontstyle
global fontsize
fontsize = val
print(setfontstyle)
def newfontstyle():
global setfontstyle
global selectedfont
global fontsize
setfontstyle = font.Font(family = selectedfont, size = fontsize)
fontsize = tk.IntVar(root)
selectedfont = tk.StringVar(root)
setfontstyle = font.Font(family=selectedfont, size=12, weight='normal')
def atributes():
while True:
global setfontstyle
global selectedfont
global fontsize
fontchoose = ["Arial", "Courier New", "Comic Sans MS", "Fixedsys", "MS Sans Serif", "MS Serif", "Symbol",
"System",
"Times New Roman", "Verdana"]
selectedfont.set("Arial")
# Variables and stuff
label1 = tk.Label(root, text="Select your options", font=setfontstyle).grid(row=1, column=3, pady=10,
sticky=tk.E + tk.W + tk.N + tk.S)
label2 = tk.Label(root, text="Font Style", font=setfontstyle).grid(row=3, column=5, pady=10,
sticky=tk.E + tk.W + tk.N + tk.S)
fontdropdown = tk.OptionMenu(root, selectedfont, *fontchoose, command=changeFont).grid(row=4, column=5, pady=10,
sticky=tk.E + tk.W + tk.N + tk.S)
label3 = tk.Label(root, text="Font Size", font=setfontstyle).grid(row=3, column=1, pady=10,
sticky=tk.E + tk.W + tk.N + tk.S)
fontsize = tk.Scale(root, from_=0, to=100, orient=tk.HORIZONTAL, command=changeSize).grid(row=4, column=1,
pady=10,
sticky=tk.E + tk.W + tk.N + tk.S)
redoall = tk.Button(root, text="Recreate", font=setfontstyle, command=newfontstyle).grid(
row=5, column=3, pady=10, sticky=tk.E + tk.W + tk.N + tk.S)
# print(fontdropdown)
root.deiconify()
# root.wait_variable(selectedfont or fontsize)
root.mainloop()
感谢您的帮助。几天来,我一直在努力找出答案。
答案 0 :(得分:1)
您无需重新创建任何内容。由于您使用的是字体对象,因此您需要做的就是修改该字体对象,使用该对象的任何字体都会立即显示新字体。
def changeFont(selection):
setfontstyle.configure(family=selection)
def changeSize(val):
setfontstyle.configure(size=val)
您的代码中还有其他错误,但这会回答您的具体问题。
这是一个简短的示例程序,它显示了当您更改字体属性时窗口小部件如何立即更改:
import tkinter as tk
import tkinter.font
font_families = [
"Arial", "Courier New", "Comic Sans MS", "Fixedsys",
"MS Sans Serif", "MS Serif", "Symbol",
"System", "Times New Roman", "Verdana"
]
def changeSize(val):
appFont.configure(size=val)
def changeFont(family):
appFont.configure(family=family)
root = tk.Tk()
root.geometry("400x200")
fontFamily = tk.StringVar(value="Arial")
fontSize = tk.IntVar(value=12)
appFont = tk.font.Font(family=fontFamily.get(), size=fontSize.get(), weight='normal')
size_widget = tk.Scale(root, from_=0, to=100, orient=tk.HORIZONTAL,
command=changeSize, variable=fontSize)
family_widget = tk.OptionMenu(root, fontFamily, *font_families, command=changeFont)
label = tk.Label(root, text="Hello, world", font=appFont)
label.pack(side="top", fill="both", expand=True)
size_widget.pack(side="bottom", fill="x", expand=False)
family_widget.pack(side="bottom", fill="x", expand=False)
root.mainloop()