Tkinter:在按钮按下时调用函数

3
import tkinter as tk

def load(event):
    file = open(textField.GetValue())
    txt.SetValue(file.read())
    file.close()

def save(event):
    file = open(textField.GetValue(), 'w')
    file.write(txt.GetValue())
    file.close()


win = tk.Tk() 
win.title('Text Editor')
win.geometry('500x500') 

# create text field
textField = tk.Entry(win, width = 50)
textField.pack(fill = tk.NONE, side = tk.TOP)

# create button to open file
openBtn = tk.Button(win, text = 'Open', command = load())
openBtn.pack(expand = tk.FALSE, fill = tk.X, side = tk.TOP)

# create button to save file
saveBtn = tk.Button(win, text = 'Save', command = save())
saveBtn.pack(expand = tk.FALSE, fill = tk.X, side = tk.TOP)

我遇到了一个错误,提示load and save are missing a position argument: event。我理解这个错误,但不知道如何解决它。

当您创建 tk.Button 时,您正在 调用 函数,因为在构造函数的调用序列中,例如将 command = load() 而不是 command = load - martineau
虽然不是完全相同的代码,但你的代码与这个问题中的代码存在相同的问题:https://dev59.com/E2025IYBdhLWcg3w1ZoL - Bryan Oakley
@martineau 我做了。现在它说 Entry 对象没有 GetValue 属性。反正,将事件作为参数放入函数中的目的是什么呢? - user7091463
4个回答

3
这里有一个可运行的答案。除了更改commmand=关键字参数以便在创建tk.Button时不调用函数之外,我还从相应的函数定义中删除了event参数,因为tkinter不会向小部件命令函数传递任何参数(而且您的函数也不需要它)。
您似乎将事件处理程序与小部件命令函数处理程序混淆了。前者在被调用时确实传递了一个event参数,但后者通常不会(除非您做了其他事情来使其发生——这是一件相当常见的需要/想要做的事情,有时被称为额外的参数技巧)。
import tkinter as tk

# added only to define required global variable "txt"
class Txt(object):
    def SetValue(data): pass
    def GetValue(): pass
txt = Txt()
####

def load():
    with open(textField.get()) as file:
        txt.SetValue(file.read())

def save():
    with open(textField.get(), 'w') as file:
        file.write(txt.GetValue())

win = tk.Tk()
win.title('Text Editor')
win.geometry('500x500')

# create text field
textField = tk.Entry(win, width=50)
textField.pack(fill=tk.NONE, side=tk.TOP)

# create button to open file
openBtn = tk.Button(win, text='Open', command=load)
openBtn.pack(expand=tk.FALSE, fill=tk.X, side=tk.TOP)

# create button to save file
saveBtn = tk.Button(win, text='Save', command=save)
saveBtn.pack(expand=tk.FALSE, fill=tk.X, side=tk.TOP)

win.mainloop()

0

从两个函数定义中移除'事件(event)'参数,并从您的命令中删除括号。


0
使用button.bind()方法,将'Button-1'作为第一个参数传递,并将函数名作为第二个参数传递。 def functionname(event): #你的代码
button = tk.Button(win, text="Login", bg="green")#, command=attempt_log_in)
button.grid(row=5, column=2)
button.bind('<Button-1>', functionname)

0
你有两个函数: saveload,它们都需要一个参数: event,但当你调用它们时。
# create button to open file
openBtn = tk.Button(win, text = 'Open', command = **load()**) <<<--- no argument in load
                                                       <<<---should be load(someEvent)
openBtn.pack(expand = tk.FALSE, fill = tk.X, side = tk.TOP)
# create button to save file
saveBtn = tk.Button(win, text = 'Save', command = **save()**)<<< ---- same for the save
saveBtn.pack(expand = tk.FALSE, fill = tk.X, side = tk.TOP)

但是在你的函数saveload中,参数event没有被使用,所以你可以直接删除事件参数,这样问题就不会出现了。像这样:

def load():
    file = open(textField.GetValue())
    txt.SetValue(file.read())
    file.close()

def save():
    file = open(textField.GetValue(), 'w')
    file.write(txt.GetValue())
    file.close()

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接