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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/41447065/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 00:58:59  来源:igfitidea点击:

Tkinter: Calling function when button is pressed

pythontkinter

提问by user7091463

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)

I get the error that load and save are missing a position argument: event. I understand the error, but don't understand how to resolve it.

我得到的错误是load and save are missing a position argument: event. 我理解错误,但不明白如何解决它。

回答by martineau

Here's a runnable answer. In addition to changing the commmand=keyword argument so it doesn't call the function when the tk.Buttons are created, I also removed the eventargument from the corresponding function definitions since tkinterdoesn't pass any arguments to widget command functions (and your function don't need it, anyway).

这是一个可运行的答案。除了更改commmand=关键字参数使其tk.Button在创建 s时不调用该函数之外,我还event从相应的函数定义中删除了该参数,因为tkinter没有将任何参数传递给小部件命令函数(并且您的函数不需要无论如何)。

You seem to be confusing event handlers with widget command function handlers. The former dohave an eventargument passed to them when they're called, but the latter typically don't (unless you do additional stuff to make it happen—it's a fairly common thing to need/want to do and is sometimes referred to as theThe extra arguments trick).

您似乎将事件处理程序与小部件命令函数处理程序混淆了。前者在被调用时确实有一个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()

回答by Niraj Trivedi

Use button.bind() mehtod and pass 'Button-1' in first argument and function name in second argument def functionname(event): #your code

使用 button.bind() mehtod 并在第一个参数中传递 'Button-1' 并在第二个参数中传递函数名称 def functionname(event): #your code

button = tk.Button(win, text="Login", bg="green")#, command=attempt_log_in)
button.grid(row=5, column=2)
button.bind('<Button-1>', functionname)

回答by Tomache George Alexandru

You have functions saveand loadthat takes 1 argument : eventbut when you called

您有函数saveload需要 1 个参数: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)

But in your function saveand loadthe argument : eventyou don't do nothing with it so you can just delete the event argumentand this problem shouldn't appear. Like this :

但是在您的函数saveload参数 : event 中,您不会对它做任何事情,因此您只需删除 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()

回答by Luke.py

Remove the 'event' arguments from both function defs and remove brackets from your commands.

从两个函数 defs 中删除“事件”参数并从命令中删除括号。