0

似乎我的 Tkinter GUI 很好,但是当加载应该激活类方法的按钮时,我会做 nadda。我最初试图在 main 中启动一个类实例: if_ name _== " main ": ed.EditorUI() 在 'ed' 下面使用了 'self' 的地方。它说 ed 未定义,这提示我试试这种方式。正如您可能知道的那样,自从我编码以来已经有几年了。非常感谢您的帮助!

    import CU_picture
    import filters
    import Tkinter

    root = Tk()
    root.title('Photo Editor Version 1.10')
    #root.iconbitmap(bitmap = '12.ico')

    #img = PhotoImage(file='12.ico')
    #root.tk.call('wm', 'iconphoto', root._w, img)

    #Adding File Menu
    menubar = Tkinter.Menu(root)

    filemenu = Tkinter.Menu(menubar, tearoff=0)
    filemenu.add_command(label="Exit", command=root.quit)
    menubar.add_cascade(label="File", menu=filemenu)

    #Display above menus
    root.config(menu=menubar)

    class EditorUI(object):

        def __init__(self):

            self.name = "EditorUI"
            self.pic = 0
            self.pix = CU_picture.Picture()
            self.filters = "No Current Filters Applied"
            self.special = "cake"

        def get_specific_color(color):

            if color == "red":
                return CU_picture.Color(255, 0, 0)
            elif color == "aqua":
                return CU_picture.Color(0, 255, 255)
            elif color == "fuschia":
                return CU_picture.Color(255, 0, 255)
            elif color == "lime":
                return CU_picture.Color(0, 255, 0)
            elif color == "yellow":
                return CU_picture.Color(255, 255, 0)

        def load():

            self.pix = get_picture()
            self.pix.show()
            self.pic = 1
            error.set("Pic Loaded: "+self.filters)
            outtext.set("New Picture - Not Saved.")
            self.filters = "No Current Filters Applied"
            self.special = "cake"
            para.set("")

        def save():

            if self.pic == 0:
                error.set("No Pic Loaded")

            else:

                name = intext.get() + ".jpg"
                self.pix.write_to(name)
                outtext.set("Saved To Given Filename.")
                para.set("")

        def negative():

            if self.pic == 0:
                error.set("No Pic Loaded")

            else:
                n = filters.NegativeFilter()
                n.apply(ed.pix)
                self.pix.show()
                para.set("")

                if self.filters == "No Current Filters Applied":
                    self.filters = "Negative-F"
                    error.set("Pic Loaded + Current Filters: "+self.filters)
                else:
                    self.filters = self.filters + ", Negative-F"
                    error.set("Pic Loaded + Current Filters: "+self.filters)

        def grayscale():

            if self.pic == 0:
                error.set("No Pic Loaded")

            else:
                g = filters.GrayScaleFilter()
                g.apply(self.pix)
                self.pix.show()
                para.set("")

                if self.filters == "No Current Filters Applied":
                    self.filters = "Grayscale-F"
                    error.set("Pic Loaded + Current Filters: "+self.filters)
                else:
                    self.filters = self.filters + ", Grayscale-F"
                    error.set("Pic Loaded + Current Filters: "+self.filters)

        def posterize():

            if self.pic == 0:
                error.set("No Pic Loaded")

            else:
                p = filters.PosterizeFilter()
                p.apply(self.pix)
                self.pix.show()
                para.set("")

                if self.filters == "No Current Filters Applied":
                    self.filters = "Posterize-F"
                    error.set("Pic Loaded + Current Filters: "+self.filters)
                else:
                    self.filters = self.filters + ", Posterize-F"
                    error.set("Pic Loaded + Current Filters: "+self.filters)

        def highlight():

            if self.pic == 0:
                error.set("No Pic Loaded")

            else:

                if intext.get() == "":
                    para.set("Input Highlight distance/Colour and try again(one at a time).")

                elif self.special == "cake" and intext.get() != "":
                    self.special = intext.get()
                    outtext.set("Highlight distance: " + self.special)
                    para.set("Input Highlight colour(red, blue, aqua, fuschia, lime, yellow) for the final step.")

                elif self.special != "cake" and intext.get() != "":
                    color = get_specific_color(intext.get())
                    h = filters.HighlightFilter()
                    h.set_distance(float(self.special))
                    h.set_color(color)
                    outtext.set("Highlight colour: " + intext.get()) 
                    h.apply(self.pix)
                    self.pix.show()

                    if self.filters == "No Current Filters Applied":
                        self.filters = "Highlight-F"
                        error.set("Pic Loaded + Current Filters: "+self.filters)
                    else:
                        self.filters = self.filters + ", Highlight-F"
                        error.set("Pic Loaded + Current Filters: "+self.filters)

        def edge():

            if self.pic == 0:
                error.set("No Pic Loaded")

            else:

               para.set("Threshold?")
                if intext.get() == "":
                    para.set("Input Threshold value and try again.")

                else:
                    para.set("Threshold: " + intext.get())
                    e = filters.EdgeFilter()
                    e.set_threshold(float(intext.get()))
                    e.apply(self.pix)
                    self.pix.show()

                    if self.filters == "No Current Filters Applied":
                        self.filters = "Edge-F"
                        error.set("Pic Loaded + Current Filters: "+self.filters)
                    else:
                        self.filters = self.filters + ", Edge-F"
                        error.set("Pic Loaded + Current Filters: "+self.filters)

        def get_picture():
            """
            Interactively select a picture file and return it.
            """

            # Pop up a dialogue box to select a file
            file = CU_picture.pick_a_file()

            # Open the file containing the picture and load it
            pict = CU_picture.load_picture(file)

            return pict

        if __name__ == "__main__":
            EditorUI()

    intext = Tkinter.StringVar()
    outtext = Tkinter.StringVar()
    error = Tkinter.StringVar()
    para = Tkinter.StringVar()

    inp = Tkinter.Entry(root, textvariable=intext)
    out = Tkinter.Label(root, textvariable=outtext)
    ER = Tkinter.Label(root, textvariable=error)
    P = Tkinter.Label(root, textvariable=para)

    load =      Tkinter.Button(root, text="Load", height=2, width=20, command=load)
    save =      Tkinter.Button(root, text="Save", height=2, width=20, command=save)
    negative =  Tkinter.Button(root, text="Negative-F", height=1, width=20, command=negative)
    grayscale = Tkinter.Button(root, text="Grayscale-F", height=1, width=20, command=grayscale)
    posterize = Tkinter.Button(root, text="Posterize-F", height=1, width=20, command=posterize)
    highlight = Tkinter.Button(root, text="Highlight-F", height=1, width=20, command=highlight)
    edge =      Tkinter.Button(root, text="Edge-F", height=1, width=20, command=edge)

    inp.grid(row=1, column=7)
    ER.grid(row=0, column= 0, columnspan=5, rowspan=2)
    out.grid(row=1, column= 0, columnspan=5, rowspan=2)
    P.grid(row=2, column= 0, columnspan=5, rowspan=2)

    load.grid(row=0, column=6)
    save.grid(row=0, column=7)
    negative.grid(row=1, column=6)
    grayscale.grid(row=3, column=7)
    posterize.grid(row=3, column=6)
    highlight.grid(row=2, column=6)
    edge.grid(row=2, column=7)

    root.mainloop()    
4

1 回答 1

1

我看到的主要问题之一(除了上面提到的@A.Rodas)会阻止按钮执行任何操作是您的按钮命令实际上并未针对类方法。他们正在寻找名为“load”、“save”等的根级可调用对象。因为这些不存在,您应该会遇到错误,但您说界面工作正常,除了按钮不合作,这不对我没有意义。

要调用类方法,您必须创建类的实例。首先修复self上面@A.Rodas 提到的问题,然后将__main__检查替换为:

editor = EditorUI()

然后修改按钮命令以调用编辑器实例上的方法:

load =      Tkinter.Button(root, text="Load", height=2, width=20, command=editor.load)
save =      Tkinter.Button(root, text="Save", height=2, width=20, command=editor.save)
negative =  Tkinter.Button(root, text="Negative-F", height=1, width=20, command=editor.negative)
grayscale = Tkinter.Button(root, text="Grayscale-F", height=1, width=20, command=editor.grayscale)
posterize = Tkinter.Button(root, text="Posterize-F", height=1, width=20, command=editor.posterize)
highlight = Tkinter.Button(root, text="Highlight-F", height=1, width=20, command=editor.highlight)
edge =      Tkinter.Button(root, text="Edge-F", height=1, width=20, command=editor.edge)

您也可以考虑在 EditorUI 类中移动所有界面创建内容除非您有其他原因将其分开。

于 2013-06-03T17:32:25.017 回答