Trouble With Classes and Functions

2019-09-16 11:32发布

I'm basically writing a module for a basic pygame drawing app. im using a Tk window to get the three color values for a custom color. i have a file that opens a Tk window and askes for 3 color values made but i cant figure how to get it all to work well and im starting to get confused

Here is my code for the Tk window:

from Tkinter import *

class Custom():
    def get_color(self):
        root = Tk()
        root.configure(background='black')
        root.wm_title("Custom")

        label1 = Label(root, text='Red Value:',bg="black", fg="white")
        label1.grid(row=2, column=0,columnspan=2)
        enter1 = Entry(root, bg='white')
        enter1.grid(row=3, column=0,columnspan=2)


        label2 = Label(root, text='Green Value:',bg="black", fg="white")
        label2.grid(row=4, column=0,columnspan=2)
        enter2 = Entry(root, bg='white')
        enter2.grid(row=5, column=0, columnspan=2)

        label3 = Label(root, text='Blue Value:',bg="black", fg="white")
        label3.grid(row=6, column=0,columnspan=2)
        enter3 = Entry(root, bg='white')
        enter3.grid(row=7, column=0, columnspan=2)

        btn1 = Button(root, text='OK', command= self.return_color, bg="black",activebackground="green", fg="white")
        btn1.grid(row=14, column=0, columnspan=2)
        label7 = Label(root, bg="black", fg = "white")
        label7.grid(row=15, column=0, columnspan=2)

        enter1.focus()

        root.mainloop()

    def return_color(self):
        try:
            r = str(self.enter1.get())
            g = str(self.enter2.get())
            b = str(self.enter3.get())
        except ValueError:
            window.label7.config(text='Enter Numbers!', fg = "red")
        root.destroy()
        return (r,g,b)

c = Custom()
c.get_color()

it works but im trying to be able to import it so i made two functions and put them in a class but now im getting confused i need to run get_color then when i click the OK button i need to run return_color i dont know if this is the way to do it ive just been trying all kinds of differnt things its saying that return_color cant get the self.enter1.get() same with enter2 and 3

here is where im giving to my draw pad program:

if key[pygame.K_c]:
    import CustomColor
    c = CustomColor.Custom()
    c.get_color()
    self.color = c.return_color()

im starting to get really confused if someone could clear this all up i would be so thank ful!!

1条回答
老娘就宠你
2楼-- · 2019-09-16 12:09

The problem is that the return value of return_color is not used, since the reference to the function passed as a command option is used to call it but not to store the result. What you can do is to store the values as attributes of the class in return_color and add a return statement in get_color after the call to start the mainloop:

def get_color()
    # Initialize the attributes with a default value
    self.r = ''
    self.g = ''
    self.b = ''
    # ...
    root.mainloop()
    return self.r, self.g, self.b

def return_color(self):
    # Entry.get returns a string, don't need to call to str()
    self.r = self.enter1.get()
    self.g = self.enter2.get()
    self.b = self.enter3.get()
    root.destroy()

Before using the color, you can check that the format is correct. Then I suggest you to rename the functions with more meaningful names; and create a Tk element, withdraw it and use a Toplevel in your class (if you create more than one Custom object, you are actually creating multiple Tk elements, and that's something which should be avoided).

查看更多
登录 后发表回答