[英]Tkinter Button does not execute command
I'm trying to create a button that opens local files from a users machine.我正在尝试创建一个从用户计算机打开本地文件的按钮。 I have my buttons set up and the function to open files is pretty simple.
我已经设置好按钮,打开文件的 function 非常简单。 When clicking the actual button, nothing actually happens.
单击实际按钮时,实际上没有任何反应。 The intended result should be a box that opens that shows local files.
预期的结果应该是一个打开的显示本地文件的框。
Here's my program so far:到目前为止,这是我的程序:
from tkinter import *
import tkinter.filedialog
gui = Tk(className='musicAi')
gui.geometry("500x500")
def UploadAction(event=None):
filename = filedialog.askopenfilename()
print('Selected:', filename)
# create button
importMusicButton = Button(gui, text='Import Music', command = UploadAction, width=40, height=3, bg='#0052cc', fg='#ffffff', activebackground='#0052cc', activeforeground='#aaffaa')
linkAccountButton = Button(gui, text='Link Account', width=40, height=3, bg='#0052cc', fg='#ffffff', activebackground='#0052cc', activeforeground='#aaffaa')
settingsButton = Button(gui, text='Settings', width=40, height=3, bg='#0052cc', fg='#ffffff', activebackground='#0052cc', activeforeground='#aaffaa')
helpButton = Button(gui, text='Help', width=40, height=3, bg='#0052cc', fg='#ffffff', activebackground='#0052cc', activeforeground='#aaffaa')
# add button to gui window
importMusicButton.pack()
linkAccountButton.pack()
settingsButton.pack()
helpButton.pack()
gui.mainloop()
Since you import filedialog
using import tkinter.filedialog
, you need to use tkinter.filedialog.askopenfilename()
to execute askopenfilename()
.由于您使用 import
filedialog
import tkinter.filedialog
,因此您需要使用tkinter.filedialog.askopenfilename()
来执行askopenfilename()
。
Change import tkinter.filedialog
to from tkinter import filedialog
or import tkinter.filedialog as filedialog
. import tkinter.filedialog
更改为from tkinter import filedialog
或import tkinter.filedialog as filedialog
。
Since you used the event object as parameters in Uploadfunction, use the bind method.由于您在 Upload 函数中使用了事件 object 作为参数,因此请使用绑定方法。
ImportMusicButton.bind(<"Button-1">, lambda:UploadAction())
In your UploadAction(event = None), remove the default value of an event parameter.在您的 UploadAction(event = None) 中,删除事件参数的默认值。 Should be
应该
def UploadAction(event):
code goes here...
I have perfect option for you.我有完美的选择给你。
Instead of using: import tkinter.filedialog
you can use a better thing.而不是使用:
import tkinter.filedialog
你可以使用更好的东西。
Use: from tkinter.filedialog import *
使用:
from tkinter.filedialog import *
Then you can remove the filedialog
from filename = filedialog.askopenfile()
.然后您可以从
filename = filedialog.askopenfile()
filedialog
删除文件对话框。
Change it to: filename = askopenfile()
:)将其更改为:
filename = askopenfile()
:)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.