How to call a function with arguments in “Button” function from “tkinter” python package? [duplicate]

北慕城南 提交于 2021-01-29 06:50:38

问题


What i want to do :

1.To create a File Dialog box with an option to select a file 1.1 First button to select file read its location ->Able to do it with the solution provided from below link

filedialog, tkinter and opening files

from tkinter import *
from tkinter.filedialog import askopenfilename
from tkinter.messagebox import showerror

class MyFrame(Frame):
    def __init__(self):
        Frame.__init__(self)
        self.master.title("Example")
        self.master.rowconfigure(5, weight=1)
        self.master.columnconfigure(5, weight=1)
        self.grid(sticky=W+E+N+S)

        self.button = Button(self, text="Browse", command=self.load_file, width=10)
        self.button.grid(row=1, column=0, sticky=W)

    def load_file(self):
        fname = askopenfilename(filetypes=(("Template files", "*.tplate"),
                                           ("HTML files", "*.html;*.htm"),
                                           ("All files", "*.*") ))
        if fname:
            try:
                print("""here it comes: self.settings["template"].set(fname)""")
            except:                     # <- naked except is a bad idea
                showerror("Open Source File", "Failed to read file\n'%s'" % fname)
            return


if __name__ == "__main__":
    MyFrame().mainloop()

1.2 Second button to start processing ->By adding another button to start ->Adding a function with argument process_it. ->So function call with argument is not working for my code

from tkinter import *
from tkinter.filedialog import askopenfilename
from tkinter.messagebox import showerror

class MyFrame(Frame):
    def __init__(self):
        Frame.__init__(self)
        self.master.title("Example")
        self.master.rowconfigure(5, weight=1)
        self.master.columnconfigure(5, weight=1)
        self.grid(sticky=W+E+N+S)

        self.button = Button(self, text="Browse", command=self.load_file, width=10)
        self.button.grid(row=1, column=0, sticky=W)

        #new code added by me:
        self.button = Button(self, text="Start Now", command=self.process_it(arg_1), width=10)
        self.button.grid(row=2, column=0, sticky=W)

    def load_file(self):
        #new code added by me:
        global arg1 

        fname = askopenfilename(filetypes=(("Template files", "*.tplate"),
                                           ("HTML files", "*.html;*.htm"),
                                           ("All files", "*.*") ))
        #new code added by me:
        arg_1 = fname

        if fname:
            try:
                print("""here it comes: self.settings["template"].set(fname)""")
            except:                     # <- naked except is a bad idea
                showerror("Open Source File", "Failed to read file\n'%s'" % fname)
            return

    # new function added by me:
    def process_it(self, arg_1):
        #use the arg_1 further, example :
        print(arg_1)    


if __name__ == "__main__":
    MyFrame().mainloop()

回答1:


You can write factory function to pass some arguments.

def command_factory(arg_1):
    def process_it():
        print(arg_1)
    return process_it

using

Button(self, text="Start Now", command=command_factory(arg_1))

or just

Button(self, text="Start Now", command=lambda: self.proccess_it(arg_1))

But in this case you need to be sure that arg_1 variable is not changing to avoid late binding.



来源:https://stackoverflow.com/questions/51305250/how-to-call-a-function-with-arguments-in-button-function-from-tkinter-python

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!