【问题标题】:my output is showing the tkinter widget error and button error and not displaying the button我的输出显示 tkinter 小部件错误和按钮错误,但不显示按钮
【发布时间】:2021-04-27 01:26:21
【问题描述】:

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

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

【问题讨论】:

    标签: python-3.x tkinter tkinter-canvas tkinter-text tkinter-button


    【解决方案1】:

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

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

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

    【讨论】:

    • 谢谢。我还以为是大错?‍♂️
    猜你喜欢
    • 2023-02-03
    • 2013-12-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-09-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多