python tkinter text'unicode'对象没有属性'get'

问题描述 投票:0回答:1

我正在尝试使用tkinter的文本框制作颜色选择器但是当我尝试在文本框中找到什么时我得到此错误'unicode' object has no attribute 'get'

这是我的代码:

def color(self): #choose a color

    def stop():# break the tkinter window
        win.destroy()

    win = Tk()
    text = Label(win, text='choose a color')
    text.grid(column=2, row=1)
    text = Label(win, text=' r     g     b ')
    text.grid(column=2, row=2)

    r = Text(win, height=1, width=3)
    r.grid(column=1, row=3)
    g = Text(win, height=1, width=3)
    g.grid(column=2, row=3)
    b = Text(win, height=1, width=3)
    b.grid(column=3, row=3)

    ok = Button(win, text='ok', command=stop)
    ok .grid(column=2, row=4)

    while True:
        r, g, b = r.get('1.0', END), g.get('1.0', END), b.get('1.0', END)
        print(r, g, b)
        win .update()

如果有帮助的话,我正在使用linux的python 3。

python python-3.x tkinter
1个回答
2
投票

您定义了名为r,g,b的输入 - 然后通过用自己的Text-result命名所述.get()s的结果来覆盖它们。

一个循环之后你再次访问它们 - 现在它们只是字符串而不再有get。

   while True:
        r, g, b = r.get('1.0', END), g.get('1.0', END), b.get('1.0', END)
        print(r, g, b)
        win .update()

固定:

   while True:
        rr, gg, bb = r.get('1.0', END), g.get('1.0', END), b.get('1.0', END)
        print(rr, gg, bb)
        win .update()

你知道how-to-debug-small-programs吗?如果没有,请阅读。如果你认为你做了,仍然读它,这是一个很好的阅读。

© www.soinside.com 2019 - 2024. All rights reserved.