【问题标题】:How to call a function with arguments in "Button" function from "tkinter" python package? [duplicate]如何从“tkinter”python包中调用带有“Button”函数参数的函数? [复制]
【发布时间】:2018-12-20 15:53:03
【问题描述】:

我想做什么:

1.创建带有选择文件选项的文件对话框 1.1 选择文件的第一个按钮读取其位置 ->能够使用以下链接提供的解决方案来做到这一点

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 第二个按钮开始处理 ->通过向开始添加另一个按钮 ->添加一个带参数的函数process_it。 ->所以带参数的函数调用不适用于我的代码

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()

【问题讨论】:

    标签: python tkinter


    【解决方案1】:

    您可以编写工厂函数来传递一些参数。

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

    使用

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

    或者只是

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

    但在这种情况下,您需要确保 arg_1 变量没有发生变化以避免后期绑定。

    【讨论】:

    • 谢谢,它正在与您提供的解决方案一起使用。
    • user9318345 将此标记为已解决。
    猜你喜欢
    • 2013-07-27
    • 2012-12-21
    • 2013-08-19
    • 2015-02-10
    • 2016-11-13
    • 1970-01-01
    • 2019-11-29
    • 2017-09-04
    • 2017-12-25
    相关资源
    最近更新 更多