我是一个新的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()
还有一个错误。
def addApp():
filename = filedialog.askopenfilename(initialdir= '/', title= "Select File",
filetypes = (("executables", "*.exe"),
("all files", "*.*")))
非常小的错误。你把命令参数加成了一个字符串,而不是一个函数。
openFile = tk.Button(root, text = "Open File", padx = 10, pady = 5, fg="black",
bg="#33F9FF", command=addApp)
EDIT: 有一个小的错别字,在 initialdir
在openfiledogbox中的参数
filename = filedialog.askopenfilename(initialdir= "/", title= "Select File",
filetypes = (("executables", "*.exe"),
("all files", "*.*")))