没有链接到Tkinter GUI的类方法

时间:2013-06-03 04:06:59

标签: class tkinter main

似乎我的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()    

1 个答案:

答案 0 :(得分:1)

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

要调用类方法,必须创建该类的实例。首先解决上面@ A.Rodas提到的self问题,然后将__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类中,除非您有其他理由将其保持独立。