按下按钮时无法更新按钮的文本

时间:2019-04-03 23:23:08

标签: python python-3.x text tkinter

当我按下另一个按钮时,我想更改一个按钮的文本,但收到以下错误消息:

Traceback (most recent call last):
  File "C:\Users\<user>\Documents\Projects\Python\geography_mc.py", line 
267, in <module>
    generate()
  File "C:\Users\<user>\Documents\Projects\Python\geography_mc.py", line 
239, in generate
    option1.config(text=capital)
AttributeError: 'NoneType' object has no attribute 'config'

我已经尝试过在线看到的两种方法(“ .config()”,我不记得的另一种方法),但是似乎都没有用。

这是我的代码:

def generate():
    global country
    global capital
    global answerPos

    countryInt = random.randint(1, 195)
    answerPos = random.randint(1, 4)
    country = list(countries)[countryInt]
    capital = countries[country]
    if answerPos == 1:
        option1.config(text=capital)
    elif answerPos == 2:
        option1.config(text=capital)
    elif answerPos == 3:
        option1.config(text=capital)
    else:
        option1.config(text=capital)

    output.config(text="Country: " + country)
    compare.config(text="")


def submit(x):

    if x == answerPos:
        compare.config(text="Correct!")
    else:
        compare.config(text="Wrong, it's " + capital)

submit1 = submit(1)
submit2 = submit(2)
submit3 = submit(3)
submit4 = submit(4)


Button(window, text="Next", command=generate).grid(row=7, column=0, padx=5, sticky=W)

option1 = Button(window, text="", command=submit1).grid(row=5, column=0, padx=5, sticky=W)
option2 = Button(window, text="", command=submit2).grid(row=5, column=0, padx=57, sticky=W)
option3 = Button(window, text="", command=submit3).grid(row=6, column=0, padx=5, sticky=W)
option4 = Button(window, text="", command=submit4).grid(row=6, column=0, padx=57, sticky=W)

generate()
window.mainloop()

据我所知,应该将按钮上的文本更改为 资本,但它不喜欢我尝试做的事情,因为它不会运行程序。如果有人能让我知道我想念的东西,那就太好了。

使用python 3.7.0和tkinter库。

0 个答案:

没有答案