如何创建交互式tkinter文本框,然后输入框,然后将文本框作为对条目的响应

时间:2018-02-15 13:26:39

标签: python tkinter

我无法首先将问题显示为文本框,而输入框则可以回答问题。按下发送按钮。然后在发送答案后弹出下一个文本框作为对输入框的响应。然后给他们另一个输入框来输入答案,用一个按钮发送答案,然后再给他们一个文本框作为答案。我是否需要使用文本框,输入框和按钮以外的其他内容。我是如此擅长使用tkinter,让我感到困惑的是如何按照自己的意愿去做。到目前为止,这是我的代码。

from tkinter import *

root = Tk()
frame = Frame(root, bg='lightgray')
frame.pack(fill='both', expand='yes')
root.title("Uganda Project")

#This is the first Question
T1= Text(frame, height=3, width=37)
T1.pack()
T1.insert(END, "Did you take your medicine?\n 1= Yes\n 2= No\n")
T1.configure(fg='white', bg='blue')

#Make an entry box to answer the question in
e1 = Entry(master=frame, fg='black', bg='lightgray', width=37)
e1.pack()

def answer1():
    """ What to do with the answer in the entry box"""
    answer1 = e1.get()
    return answer1

#Make a Send button to send the answer from the entry box to the definition 
of answer1 
send = Button(frame, text="Send", command=answer1)
send.pack()

#This is the second question
if answer1==1:       
    T2 = Text(frame, height=1, width=37)
    T2.pack()
    T2.insert(END, "Good Job! Text 0 to end conversation")
    T2.configure(fg='white', bg='blue')

elif answer1==2:
    T2 = Text(frame, height=3, width=37)
    T2.pack()
    T2.insert(END, "1= Did you forget?\n2= Did you run out of medicine?\n3= 
    Did it have bad side effects?\n")
    T2.configure(fg='white', bg='blue')

else:
    T2 = Text(frame, height=3, width=37)
    T2.pack()
    T2.insert(END, "I did not understand your response.")
    T2.configure(fg='white', bg='blue')

#Make an entry box to answer the question in
e2 = Entry(master=frame, fg='black', bg='lightgray', width=37)
e2.pack()

def answer2():
    """ What to do with the answer in the entry box"""
    answer2 = e2.get()
    return answer2

#Make a Send button to send the answer from the entry box to the definition 
of answer2 
send = Button(frame, text="Send", command=answer2)
send.pack()

#This is the response to the second entry box
if answer2==0:
    T3 = Text(frame, height=1, width=37)
    T3.pack()
    T3.insert(END, "Have a good day.")
    T3.configure(fg='white', bg='blue')

elif answer2==1:
    T3 = Text(frame, height=2, width=37)
    T3.pack()
    T3.insert(END, "Take it as your earliest convience.\nDo Not Take two 
    doses at once!\n")
    T3.configure(fg='white', bg='blue')

elif answer2==2:
    T3 = Text(frame, height=2, width=37)
    T3.pack()
    T3.insert(END, "Please contact your doctor or\npharmacy for refills.\n")
    T3.configure(fg='white', bg='blue')

elif answer2==3:
    T3 = Text(frame, height=3, width=37)
    T3.pack()
    T3.insert(END, "Please contact your doctor about\nchanging your 
    medications or\ndealing with your symptoms.\n")
    T3.configure(fg='white', bg='blue')
else:
    T3 = Text(frame, height=3, width=37)
    T3.pack()
    T3.insert(END, "I did not understand your response.")
    T3.configure(fg='white', bg='blue')

root.mainloop()

2 个答案:

答案 0 :(得分:0)

请尝试此代码(我已经更改了一些内容以及添加其他内容以帮助完成此项工作)。它不是最有效或最好的结构化代码,但它完成了工作(至少对我来说):

from tkinter import *
import datetime

root = Tk ()
root.bind ("<Return>", lambda event: answer1 ())
f = Frame(root, bg='lightgray')
f.grid (sticky = "nsew")
Grid.columnconfigure (root, 0, weight = 1)
Grid.rowconfigure (root, 0, weight = 1)
root.title("Uganda Project")

#This is the first Question
Bar = Scrollbar (f)
T = Listbox (f, fg='white', bg='blue', yscrollcommand = Bar.set, width = 50, height = 20)
Bar.config (command = T.yview)
T.grid (row = 0, column = 0, columnspan = 2, sticky = "nsew")
Bar.grid (row = 0, column = 2, sticky = "ns")
Grid.columnconfigure (f, 0, weight = 1)
Grid.rowconfigure (f, 0, weight = 1)
T.insert (END, "Support:")
T.insert (END, "    Did you take your medicine?")
T.insert (END, "    1= Yes")
T.insert (END, "    2= No")

#Make an entry box to answer the question in
e = Entry(f, fg='black', bg='lightgray')
e.focus_force ()
e.grid (row = 1, column = 0, sticky = "ew")

def answer1():
    """ What to do with the answer in the entry box"""
    answer1 = e.get()
    T.insert (END, "You: " + answer1)
    e.delete (0, END)
    if answer1=="1":       
        T.insert (END, "Support:")
        T.insert (END, "    Good Job! Text 0 to end conversation")
        send.config (command = answer2)
        root.bind ("<Return>", lambda event: answer2 ())

    elif answer1=="2":
        T.insert (END, "Support:")
        T.insert (END, "    1= Did you forget?")
        T.insert (END, "    2= Did you run out of medicine?")
        T.insert (END, "    3= Did it have bad side effects?")
        send.config (command = answer2)
        root.bind ("<Return>", lambda event: answer2 ())

    else:
        T.insert (END, "Support:")
        T.insert (END, "    I did not understand your response.")

    return answer1

#Make a Send button to send the answer from the entry box to the definition of answer1 
send = Button(f, text="Send", command=answer1)
send.grid(row = 1, column = 1, columnspan = 2)

#This is the second question

#Make an entry box to answer the question in

def save ():
    with open ("Log - %s.txt" % str (datetime.datetime.now ()).replace (":", "."), "w") as f: f.write ("\n".join (T.get (0, END)))

def answer2():
    """ What to do with the answer in the entry box"""
    answer2 = e.get()
    T.insert (END, "You: " + answer2)
    e.delete (0, END)
    #This is the response to the second entry box
    if answer2=="0":
        T.insert (END, "Support:")
        T.insert (END, "    Have a good day.")
        send.config (text = "OK", command = root.destroy)
        root.bind ("<Return>", lambda event: root.destroy ())
        save ()
        e.pack_forget ()

    elif answer2=="1":
        T.insert (END, "Support:")
        T.insert (END, "    Take it as your earliest convience.")
        T.insert (END, "    Do Not Take two doses at once!")
        root.bind ("<Return>", lambda event: root.destroy ())
        send.config (text = "OK", command = root.destroy)
        save ()
        e.pack_forget ()

    elif answer2=="2":
        T.insert (END, "Support:")
        T.insert (END, "    Please contact your doctor or")
        T.insert (END, "    pharmacy for refills.\n")
        root.bind ("<Return>", lambda event: root.destroy ())
        send.config (text = "OK", command = root.destroy)
        save ()
        e.pack_forget ()

    elif answer2=="3":
        T.insert (END, "Support:")
        T.insert (END, "    Please contact your doctor about")
        T.insert (END, "    changing your medications or")
        T.insert (END, "    dealing with your symptoms.")
        root.bind ("<Return>", lambda event: root.destroy ())
        send.config (text = "OK", command = root.destroy)
        save ()
        e.pack_forget ()

    else:
        T.insert (END, "Support:")
        T.insert (END, "    I did not understand your response.")

    return answer2

root.mainloop ()


#Make a Send button to send the answer from the entry box to the definition of answer1 
send = Button(f, text="Send", command=answer1)
send.grid(row = 1, column = 1, columnspan = 2)

#This is the second question

#Make an entry box to answer the question in

def answer2():
    """ What to do with the answer in the entry box"""
    answer2 = e.get()
    T.insert (END, "You: " + answer2)
    e.delete (0, END)
    #This is the response to the second entry box
    if answer2=="0":
        T.insert (END, "Support:")
        T.insert (END, "    Have a good day.")
        send.config (text = "OK", command = root.destroy)
        root.bind ("<Return>", lambda event: root.destroy ())
        e.pack_forget ()

    elif answer2=="1":
        T.insert (END, "Support:")
        T.insert (END, "    Take it as your earliest convience.")
        T.insert (END, "    Do Not Take two doses at once!")
        root.bind ("<Return>", lambda event: root.destroy ())
        send.config (text = "OK", command = root.destroy)
        e.pack_forget ()

    elif answer2=="2":
        T.insert (END, "Support:")
        T.insert (END, "    Please contact your doctor or")
        T.insert (END, "    pharmacy for refills.\n")
        root.bind ("<Return>", lambda event: root.destroy ())
        send.config (text = "OK", command = root.destroy)
        e.pack_forget ()

    elif answer2=="3":
        T.insert (END, "Support:")
        T.insert (END, "    Please contact your doctor about")
        T.insert (END, "    changing your medications or")
        T.insert (END, "    dealing with your symptoms.")
        root.bind ("<Return>", lambda event: root.destroy ())
        send.config (text = "OK", command = root.destroy)
        e.pack_forget ()

    else:
        T.insert (END, "Support:")
        T.insert (END, "    I did not understand your response.")

    return answer2

root.mainloop()

下面是一个假设如何调用不同项目的示例:

1)项目与调用代码

位于同一目录中

2)项目以其功能命名(分别为Uganda Project

我还创建了一个Rwanda Project,以展示如何添加除Uganda Project以外的额外项目。

from tkinter import *
import importlib, threading, time, logging

PROJECTS = ("Uganda Project", "Rwanda Project")

def constructWindow ():
    global root, close
    close, wait = False, False
    logging.basicConfig (format = "%(message)s")
    while True:
        root = Tk ()
        root.title ("Main Menu")
        Label (root, text = "Which project would you like to run?").grid ()
        for project in PROJECTS: Button (root, text = project, command = lambda project = project: callProject (project)).grid ()
        root.protocol ("WM_DELETE_WINDOW", closeWindow)
        root.mainloop ()
        while wait: time.sleep (0.1)
        if close: break
def callProject (project):
    global wait
    wait = True
    root.destroy ()
    try: importlib.import_module (project)
    except ImportError: logging.warn ("Cannot find project.")
    wait = False
def closeWindow ():
    global close
    close = True
    root.destroy ()
if __name__ == "__main__": constructWindow ()

答案 1 :(得分:0)

使用以前的界面再添加一个代码:

import tkinter as tk
root = tk.Tk()
root.title("Uganda Project")
frame = tk.Frame(root, bg='lightgray')
frame.pack(fill='both', expand='yes')

def answer1():
    """ What to do with the answer in the entry box"""
    # This is the second question
    if e1.get () == str (1):
        T2.replace (1.0, tk.END, "Good Job! Text 0 to end conversation")
    elif e1.get () == str (2):
        T2.replace (1.0, tk.END, "1= Did you forget?\n"
                                 "2= Did you run out of medicine?\n"
                                 "3= Did it have bad side effects?\n")

def answer2():
    """ What to do with the answer in the entry box"""
    #This is the response to the second entry box
    if e2.get()==str(0):
        T3.replace (1.0, tk.END, "Have a good day.")
    elif e2.get()==str(1):
        T3.replace (1.0, tk.END, "Take it as your earliest convience.\n"
                                 "Do Not Take two doses at once!\n")
    elif e2.get()==str(2):
        T3.replace (1.0, tk.END, "Please contact your doctor or\n"
                                 "pharmacy for refills.\n")
    elif e2.get()==str(3):
        T3.replace (1.0, tk.END, "Please contact your doctor about\n"
                                 "changing your medications or\n"
                                 "dealing with your symptoms.\n")

#This is the first Question
T1= tk.Text(frame, height=3, width=37)
T1.pack()
T1.insert(tk.END, "Did you take your medicine?\n 1= Yes\n 2= No\n")
T1.configure(fg='white', bg='blue')

#Make an entry box to answer the question in
e1 = tk.Entry(master=frame, fg='black', bg='lightgray', width=37)
e1.pack()

#Make a Send button to send the answer from the entry box to the definition of answer1
send = tk.Button(frame, text="Send", command=answer1)
send.pack()

T2 = tk.Text(frame, height=3, width=37)
T2.pack()
T2.insert(tk.END, "I did not understand your response.")
T2.configure(fg='white', bg='blue')

#Make an entry box to answer the question in
e2 = tk.Entry(master=frame, fg='black', bg='lightgray', width=37)
e2.pack()

T3 = tk.Text(frame, height=3, width=37)
T3.pack()
T3.insert(tk.END, "I did not understand your response.")
T3.configure(fg='white', bg='blue')

#Make a Send button to send the answer from the entry box to the definition of answer2
send2 = tk.Button(frame, text="Send", command=answer2)
send2.pack()

root.mainloop()
相关问题