Tkinter的root返回属性错误

时间:2015-06-15 08:50:14

标签: python tkinter

from tkinter import *
import random

root=Tk()
answers=["It is certain. ", "signs point to yes.","My sources say no.","Not a chance.","Dumb Question ,ask another."]
error_messages=["Was That A Question ,Stupid Head", "You know that squiggly thing with a dot underneath? Its a Question Mark","You forgot a question mark","Dident anybody teach you to use a question mark?"]
all_numbers=["err,42?","Please ask me a proper question ","That just looked like a number to me? ","i am a talking robot not a calculator","I need the question in words ,Stupid head"]

def submit(*ignore):
    """This Function takes in the question and produces an answer"""
question=ask_box.get()

if question =="":
    message=("you didn't ask anything")
elif question.isdigit:
    message=random.choice(all_numbers)
elif not question.endwith("?"):
    message=random.choice(error_message)
elif not " "in questions:
    message=random.choice=("I dont like one word questions")
display(message)

def display(message):
"""Simple text display function.Takes a String"""
ask_box.delete(0,END)
txt["state"]="normal"
txt.delete(0,0,END)
txt.insert(0,0,message)
txt["state"]="disabled"

#set up the GUI
root.Tk()
root.resizable(0,0)
app=Frame(root)
app.grid()
root.bind('<Return>',submit)

root.title("Magic 8 Ball")
root.geometry("220x325")

ask_lbl=Label(app,text="Ask a Yes or No question")
ask_lbl.grid(row=0,column=0,columnspan=3,pady=5)

ask_box=Entry(app,width=30)
ask_box.grid(row=1,column=0,columnspan=3,pady=5)
ask_box.focus_force()

shake=Button(app,text="Shake the 8 ball")
shake["command"]=submit
shake.grid(row=2,column=1,pady=5)

txt=Text(app,width=30,height=3,wrap=WORD,state=DISABLED)
txt.grid(row=4,column=0,columnspan=3,pady=5)

#start the loop
root.mainloop()

这段代码应该像魔术8球一样工作,但是tkinter不断返回属性错误任何人都可以解释原因,谢谢。程序是给了我一个项目但我不能让tkinter运行程序

2 个答案:

答案 0 :(得分:1)

只需删除第root.Tk()行即可。这可能应该是root=Tk(),但是你已经在代码的最开头那样做了,所以没有必要。

答案 1 :(得分:0)

如果你以root身份导入tkinter ...就像“import tkinter as root”那么你必须添加该语句root.Tk()。但是你刚刚将tkinter导入为“来自tkinter import *”,所以你不需要通过一个对象访问Tk()......你可以直接调用它...