简体   繁体   English

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

[英]Python Tkinter - add external function as command in menu

I'm having a problem with Tkinter menu. 我的Tkinter菜单有问题。 Here is the code for my gui.py file: 这是我的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()

And here is the code for my file.py: 这是我的file.py的代码:

from tkinter import *
from tkinter.filedialog import askopenfilename

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

The problem is, when I run the gui.py file, the file dialogue always appears before the menu, and when I close it and try to access it through the menu Open, nothing happens. 问题是,当我运行gui.py文件时,文件对话框总是出现在菜单之前,当我关闭它并尝试通过菜单Open访问它时,没有任何反应。 What did I do wrong here? 我在这做错了什么? Please help and thanks in advance. 请提前帮助和感谢。

Commands should give the name of the method. 命令应该给出方法的名称。 What you're doing is calling the method. 你正在做的是调用方法。 Tkinter will then use the return value of that method as the button command. 然后,Tkinter将使用该方法的返回值作为按钮命令。 Solution: leave out the brackets. 解决方案:省略括号。

..., command=file.open

instead of 代替

..., command=file.open()

You did it right for the exit button though! 你做了正确的退出按钮!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM