-1

以下是我创建待办事项列表应用程序的代码

from tkinter import ...
// i have just not mentioned all imports   
root = Tk()
root.title("My Todo List App") 
root.configure(background="light green")   
root.geometry("550x500")    
root.resizable(width=False , height=False)   
title_label = ttk.Label(root, text="Title",background="light green",font=("TkDefaultFont",16))
title_label.grid(row=0, column=0, sticky=W)
title_text = StringVar()
title_entry = ttk.Entry(root, width=25, textvariable=title_text)
title_entry.grid(row=0, column=1, sticky=W)
add_btn = Button(root, text="Add Task", bg="blue", fg="white", font="helvetica 1- bold", command="")
add_btn.grid(row=0, column=2, sticky=W)

       root.mainloop()     

我在运行上述代码时在 Visual Studio 代码中遇到的错误是:

C:\Users\tkurd\AppData\Local\Programs\Python\Python39\python.exe C:/Users/tkurd/Desktop/todolist/todo.py
  
Traceback (most recent call last):

  File "C:\Users\tkurd\Desktop\todolist\todo.py", line 22, in <module>
    add_btn = Button(root, text="Add Task", bg="blue", fg="white", font="helvetica 1- bold", command="")

 File "C:\Users\tkurd\AppData\Local\Programs\Python\Python39\lib\tkinter\__init__.py", line 2650, in __init__
    Widget.__init__(self, master, 'button', cnf, kw)

 File "C:\Users\tkurd\AppData\Local\Programs\Python\Python39\lib\tkinter\__init__.py", line 2572, in __init__
    self.tk.call(
        _tkinter.TclError: expected integer but got "1-"

 Process finished with exit code 1
4

1 回答 1

1

在 Tkinter 中指定字体时,我们不包括连字符 (-)。只能使用空格,并且文本只能是 a) 字体大小 b) 字体名称 c) 粗体、斜体或下划线。

所以将第 23 行替换为: add_btn = Button(root, text="Add Task", bg="blue", fg="white", font="helvetica 1 bold", command="")

注意:可能看起来按钮的文本消失了,但只是字体大小为 1。

于 2021-04-27T01:39:57.590 回答