类和函数的麻烦 [英] Trouble With Classes and Functions

查看:173
本文介绍了类和函数的麻烦的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我基本上是为一个基本的pygame绘图应用程序编写一个模块。 im使用Tk窗口获得自定义颜色的三个颜色值。我有一个文件,打开一个Tk窗口,要求提供3个颜色值,但我无法想象如何让它一切正常,并开始困惑

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

这里是我的代码Tk窗口:

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()

它可以工作,但我想能够导入它所以我做了两个函数,并把它们放在一个类,但现在不知道我需要运行 get_color 然后当我单击确定按钮我需要运行 return_color 我不知道这是否是这样做的ive只是一直在尝试各种不同的东西,它说: return_color 不能得到 self.enter1.get()与enter2和3

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()



如果有人可以清除这一切,我真的很困惑我会感谢ful !!


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

推荐答案

问题是 return_color 的返回值未使用,因为对作为命令选项传递的函数的引用用于调用它,但不存储结果。您可以做的是将值存储为 return_color 中的类的属性,并在 get_color 中添加return语句启动mainloop的调用:

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()

在使用颜色之前,您可以检查格式是否正确。然后我建议你用更有意义的名字重命名函数;并创建一个Tk元素,撤消它并在你的类中使用Toplevel(如果你创建多个Custom对象,你实际上是创建多个Tk元素,这是应该避免的)。

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).

这篇关于类和函数的麻烦的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆