Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/353.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python Tkinter-在菜单中添加外部函数作为命令_Python_Menu_Command_Tkinter_External - Fatal编程技术网

Python Tkinter-在菜单中添加外部函数作为命令

Python Tkinter-在菜单中添加外部函数作为命令,python,menu,command,tkinter,external,Python,Menu,Command,Tkinter,External,我对Tkinter菜单有问题。以下是我的gui.py文件的代码: from tkinter import * from SS2 import file class AppUI(Frame): def __init__(self, master=None): Frame.__init__(self, master, relief=SUNKEN, bd=2) self.menubar = Menu(self) menu = Menu(s

我对Tkinter菜单有问题。以下是我的gui.py文件的代码:

from tkinter import *
from SS2 import file

class AppUI(Frame):

    def __init__(self, master=None):
        Frame.__init__(self, master, relief=SUNKEN, bd=2)

        self.menubar = Menu(self)

        menu = Menu(self.menubar, tearoff=0)
        self.menubar.add_cascade(label="File", menu=menu)
        menu.add_command(label="Open", command=file.open())
        menu.add_command(label="Save")
        menu.add_command(label="Save as...")
        menu.add_command(label="Exit",
                         command=root.quit)

        menu = Menu(self.menubar, tearoff=0)
        self.menubar.add_cascade(label="Image", menu=menu)
        menu.add_command(label="Size")
        menu.add_command(label="Rotate")
        menu.add_command(label="Crop")

        menu = Menu(self.menubar, tearoff=0)
        self.menubar.add_cascade(label="Color", menu=menu)

        menu = Menu(self.menubar, tearoff=0)
        self.menubar.add_cascade(label="Filter", menu=menu)
        menu.add_command(label="Blur")
        menu.add_command(label="Contour")
        menu.add_command(label="Emboss")
        menu.add_command(label="Smooth")
        menu.add_command(label="Sharpen")

        menu = Menu(self.menubar, tearoff=0)
        self.menubar.add_cascade(label="Help", menu=menu)
        menu.add_command(label="About")

        try:
            self.master.config(menu=self.menubar)
        except AttributeError:
            # master is a toplevel window (Python 1.4/Tkinter 1.63)
            self.master.tk.call(master, "config", "-menu", self.menubar)

        self.canvas = Canvas(self, bg="white", width=400, height=400,
                             bd=0, highlightthickness=0)
        self.canvas.pack()


root = Tk()

app = AppUI(root)
app.pack()

root.mainloop()
下面是my file.py的代码:

from tkinter import *
from tkinter.filedialog import askopenfilename

def open():
    filename = askopenfilename(filetypes=[("allfiles","*"),("imagesfiles","*.png")])

问题是,当我运行gui.py文件时,文件对话框总是出现在菜单之前,当我关闭它并试图通过“打开”菜单访问它时,什么也没有发生。我做错了什么?请提前提供帮助和感谢。

命令应给出方法的名称。您正在做的是调用该方法。Tkinter随后将使用该方法的返回值作为按钮命令。解决方案:去掉括号

..., command=file.open
而不是

..., command=file.open()
不过你在退出按钮上做得对