单击时会打开新窗口但不会显示任何内容[Tkinter]

时间:2018-05-21 09:34:04

标签: python-2.7 tkinter

我使用root作为主窗口,当提出问题时,Wikipedia或WolframAlpha的答案会显示在新窗口中。但是,这里发生的事情是新窗口正确打开但没有显示任何内容。

from Tkinter import *
import wolframalpha
import wikipedia

root=Tk()
root1=Tk()

def getinput():
    global entry
    answer = StringVar()
    ques=entry.get()
    try:
        #wolframalpha
        app_id = myappid #value of myappid is there in the original code
        client = wolframalpha.Client(app_id)
        res = client.query(ques)
        answer.set(next(res.results).text)
        label=Label(root1, textvariable=answer)

    except:
        #wikipedia
        answer.set(wikipedia.summary(ques).encode('utf-8'))
        label=Label(root1, textvariable=answer)
    label.pack(side=BOTTOM)

root.geometry("350x80+300+300")
label=Label(root, text="Hi! I am Python Digital Assistant. How can I help you today?")
entry=Entry(root)
submit=Button(root, text="Submit", bg="light green", command=getinput)

exit1=Button(root, text="Exit", bg="red", fg="white", command=root.destroy)

label.pack()
entry.pack(fill=X)
entry.focus_set()
submit.pack(side=LEFT)
exit1.pack(side=LEFT)
root.mainloop()

1 个答案:

答案 0 :(得分:0)

您无需致电TK两次,您必须使用toplevel来实现这一目标,当您提供问题并点击submit方法时,答案将会弹出在Toplevel窗口中。

from Tkinter import *
import wolframalpha
import wikipedia

root=Tk()


def getinput():

    top = Toplevel()
    top.geometry("500x500")
    global entry
    answer = StringVar()
    ques=entry.get()
    try:
        #wolframalpha
        app_id = myappid #value of myappid is there in the original code
        client = wolframalpha.Client(app_id)
        res = client.query(ques)
        answer.set(next(res.results).text)
        label=Label(top, textvariable=answer)

    except:
        #wikipedia
        answer.set(wikipedia.summary(ques).encode('utf-8'))
        label=Label(top, textvariable=answer)
    label.pack(side=TOP)

root.geometry("350x80+300+300")
label=Label(root, text="Hi! I am Python Digital Assistant. How can I help you today?")
entry=Entry(root)
submit=Button(root, text="Submit", bg="light green", command=getinput)

exit1=Button(root, text="Exit", bg="red", fg="white", command=root.destroy)

label.pack()
entry.pack(fill=X)
entry.focus_set()
submit.pack(side=LEFT)
exit1.pack(side=LEFT)
root.mainloop()

result