【问题标题】:Can't click button on tkinter无法单击 tkinter 上的按钮
【发布时间】:2020-09-21 11:14:04
【问题描述】:

我是 tkinter 的新手,我在使用 python GUI 上的按钮时遇到问题。我创建了按钮及其背后的功能,但我无法单击该按钮。有人可以告诉我我做错了什么吗? UI 出现了,我可以看到我无法点击的按钮。

import tkinter as tk

from tkinter import filedialog, Text

import os


#main
root = tk.Tk()

def addApp():
    filename = filedialog.askopenfilename(initialir= "/", title= "Select File",
                                          filetypes = (("executables", "*.exe"),
                                                        ("all files", "*.*")))

canvas = tk.Canvas(root, height=700, width=700, bg="#33F9FF")

canvas.pack()

frame = tk.Frame(root, bg="white")
frame.place(relwidth=0.8, relheight=0.8, relx = 0.1, rely = 0.1)

openFile = tk.Button(root, text = "Open File", padx = 10, pady = 5, fg="black",
                     bg="#33F9FF", command="addApp")

openFile.pack()

runApps = tk.Button(root, text = "Run Apps", padx = 10, pady = 5, fg="black",
                    bg="#33F9FF")

runApps.pack()

root.mainloop()

【问题讨论】:

  • command=addApp
  • 这引发了一堆错误。仍然不允许我实际点击按钮
  • 不太确定此错误是否与先前评论的编辑有关。
  • command 选项需要可调用函数,而不是字符串。
  • 明白了,我才意识到这一点。我现在进行了更改,只是遇到了 Tkinter 回调的一些异常。谢谢!

标签: python user-interface button tkinter


【解决方案1】:

非常小的错误。您将命令参数添加为字符串而不是函数

openFile = tk.Button(root, text = "Open File", padx = 10, pady = 5, fg="black",
                     bg="#33F9FF", command=addApp)

编辑: openfiledialogbox 中的initialdir 参数有一个小错字

filename = filedialog.askopenfilename(initialdir= "/", title= "Select File",
                                          filetypes = (("executables", "*.exe"),
                                                        ("all files", "*.*")))

【讨论】:

    【解决方案2】:

    还有一个错误:

    def addApp():
        filename = filedialog.askopenfilename(initialdir= '/', title= "Select File",
                                              filetypes = (("executables", "*.exe"),
                                                            ("all files", "*.*")))
    

    【讨论】:

    • 就是这样。谢谢你们!我真的要检查我的眼睛哈哈
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-05-15
    • 2021-12-24
    • 1970-01-01
    相关资源
    最近更新 更多