使用 Tkinter 创建 UI。当尝试调整我的按钮文本的字体和我 运行 程序时,我得到了这个错误
Creating a UI with Tkinter. When trying to adjust the font of my button text and I run the program, I get this error back
这是我到目前为止编写的代码...
import tkinter as tk
import sys
import tkinter.font as font
from tkinter.ttk import *
app = tk.Tk()
app.geometry("400x400")
app.configure(bg='gray')
photo = tk.PhotoImage(file=r"C:\ex\ex_button_active.png")
myFont = font.Font(family='Helvetica', size=20, weight='bold')
tk.Label(app, text='ex', bg='gray', font=(
'Verdana', 15)).pack(side=tk.TOP, pady=10)
app.iconbitmap(r'C:\ex\ex_icon.ico')
ex_activation_button = tk.Button(app,
bg='black',
image=photo,
width=100,
height=100)
ex_stop_button = tk.Button(app,
bg='Gray',
text='Stop',
width=15,
height=5)
tk.Button['font'] = myFont
app.title("ex")
ex_activation_button.pack(side=tk.TOP)
ex_stop_button.pack(side=tk.LEFT)
app.mainloop()
我收到了这个错误...
tk.Button['font'] = myFont
TypeError: 'type' object does not support item assignment
对于按钮上的文本来说,这确实是一个简单的字体更改。非常感谢任何帮助!
tk.Button
是 类型 ,如 int
或 float
等。如果你这样做,你会得到完全相同的错误 int['font'] = myFont
.
相反,您要做的是在 tk.Button
的 实例 上设置字体。在你的情况下是 ex_stop_button
:
ex_stop_button['font'] = myFont
-或-
ex_stop_button = tk.Button(app,
bg='Gray',
text='Stop',
width=15,
height=5,
font=myFont)
这是我到目前为止编写的代码...
import tkinter as tk
import sys
import tkinter.font as font
from tkinter.ttk import *
app = tk.Tk()
app.geometry("400x400")
app.configure(bg='gray')
photo = tk.PhotoImage(file=r"C:\ex\ex_button_active.png")
myFont = font.Font(family='Helvetica', size=20, weight='bold')
tk.Label(app, text='ex', bg='gray', font=(
'Verdana', 15)).pack(side=tk.TOP, pady=10)
app.iconbitmap(r'C:\ex\ex_icon.ico')
ex_activation_button = tk.Button(app,
bg='black',
image=photo,
width=100,
height=100)
ex_stop_button = tk.Button(app,
bg='Gray',
text='Stop',
width=15,
height=5)
tk.Button['font'] = myFont
app.title("ex")
ex_activation_button.pack(side=tk.TOP)
ex_stop_button.pack(side=tk.LEFT)
app.mainloop()
我收到了这个错误...
tk.Button['font'] = myFont
TypeError: 'type' object does not support item assignment
对于按钮上的文本来说,这确实是一个简单的字体更改。非常感谢任何帮助!
tk.Button
是 类型 ,如 int
或 float
等。如果你这样做,你会得到完全相同的错误 int['font'] = myFont
.
相反,您要做的是在 tk.Button
的 实例 上设置字体。在你的情况下是 ex_stop_button
:
ex_stop_button['font'] = myFont
-或-
ex_stop_button = tk.Button(app,
bg='Gray',
text='Stop',
width=15,
height=5,
font=myFont)