I have written the following Tkinter script:

When I run it, the first thing that happens is the File Dialog box opens without it being called.
I am expecting the file dialog box only to open when the "Open Document" button is pressed.

import tkinter as tk
from tkinter import *
from tkinter import filedialog as fd


class Application(tk.Frame):
    def __init__(self, master=None):
        super().__init__(master)
        self.master = master
        self.pack()
        self.do = PhotoImage(file="do.png")  # Document Open
        self.ea = PhotoImage(file="ea.png")  # Exit
        self.create_widgets()

    def create_widgets(self):
        self.opendoc = tk.Button(self, text="Open Document", image=self.do, command=self.openfd()).pack(side="left")

        self.quit = tk.Button(self, text="Quit", image=self.ea, command=self.master.destroy)
        self.quit.pack(side="left")

    def openfd(self):
        fd.askopenfilenames()

if __name__ == '__main__':
    root = tk.Tk()
    app = Application(master=root)
    app.mainloop()

I have tried removing the openfd function from the class, but the reult is the same.

import tkinter as tk
from tkinter import *
from tkinter import filedialog as fd


class Application(tk.Frame):
    def __init__(self, master=None):
        super().__init__(master)
        self.master = master
        self.pack()
        self.do = PhotoImage(file="do.png")  # Document Open
        self.ea = PhotoImage(file="ea.png")  # Exit
        self.create_widgets()

    def create_widgets(self):
        self.opendoc = tk.Button(self, text="Open Document", image=self.do, command=openfd()).pack(side="left")

        self.quit = tk.Button(self, text="Quit", image=self.ea, command=self.master.destroy)
        self.quit.pack(side="left")


    def setdevicevars(self):
        self.settingsDialog()

def openfd():
    fd.askopenfilenames()
if __name__ == '__main__':
    root = tk.Tk()
    app = Application(master=root)
    app.mainloop()

It's not necessarily a problem, but I'd like to know why, not least to prevent unexpected behaviuor in my script.

Okay.

It seems as though, instead of typing myFunction(arg), I need to type in lambda: myFunction(arg)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.