我正在尝试为用户创建一个返回yes或no的代码,但输出始终为yes。
我应该更改或添加什么才能更改答案?
from tkinter import *
from tkinter import ttk
import random
list=["yes", "no"] #a list of answers
answer=random.choice(list) #choosing an answer
root=Tk(className="yes or no")
lab=ttk.Label(root, text="what's your question" )
lab.pack()
#ask user for input
U=ttk.Entry(root)
U.pack()
def return_answer():
U.get()
U.delete(0, END)
U.insert(0,answer)
B1=ttk.Button(root, text="ok",command=return_answer)
B1.pack()
root.mainloop()
答案 0 :(得分:2)
删除
answer=random.choice(list) #choosing an answer
并替换
U.insert(0,answer)
与
U.insert(0, random.choice(list))