与tkinter讨论

时间:2015-08-05 01:04:49

标签: python tkinter

from tkinter import *

window = Tk()

ia_answers= "trolol"
input_frame = LabelFrame(window, text="User :", borderwidth=4)
input_frame.pack(fill=BOTH, side=BOTTOM)

input_user = StringVar()
input_field = Entry(input_frame, text=input_user)
input_field.pack(fill=BOTH, side=BOTTOM)

ia_frame = LabelFrame(window, text="Discussion",borderwidth = 15, height = 100, width = 100)
ia_frame.pack(fill=BOTH, side=TOP)

user_says = StringVar()
user_text = Label(ia_frame, textvariable=user_says, anchor = NE, justify =   RIGHT, bg="white") 
user_text.pack(fill=BOTH, side=TOP) 

ia_says = StringVar()
ia_text = Label(ia_frame, textvariable=ia_says, anchor = W, justify = LEFT, bg="white") 
ia_text.pack(fill=BOTH, side=BOTTOM) 

def Enter_pressed(event):
    """Took the current string in the Entry field."""
    input_get = input_field.get()
    input_user.set("")
    user_says.set(input_get + "\n\n")
    ia_says.set(ia_answers)

input_field.bind("<Return>", Enter_pressed)
window.mainloop()

嗨,我正在尝试建立一个讨论Bot。

当我执行代码时,输​​入字段中的问题和答案会正确显示。

问题是在输入下一句后,上一个问题/答案被删除。这是一个例子:

Hello Bot
Hello User
(then the text disappears)
How are you
Fine thank you

我想要的是什么:

Hello Bot
Hello User
(then the text stays in the frame)
How are you
Fine thank you

1 个答案:

答案 0 :(得分:0)

问题出现在行 -

user_says.set(input_get + "\n\n")
ia_says.set(ia_answers)

您将替换users_says.set()ia_says.set()使用新值重置整个标签。相反,您应该获取旧值并将新值附加到它并将其重新设置,例如 -

user_says.set(user_says.get() + input_get + "\n")
ia_says.set(ia_says.get() + ia_answers + "\n")

或者您也可以为每个新活动创建一个新标签,并将其添加到LabelFrame。示例 -

from tkinter import *

window = Tk()

ia_answers= "trolol\n"
input_frame = LabelFrame(window, text="User :", borderwidth=4)
input_frame.pack(fill=BOTH, side=BOTTOM)

input_user = StringVar()
input_field = Entry(input_frame, text=input_user)
input_field.pack(fill=BOTH, side=BOTTOM)

ia_frame = LabelFrame(window, text="Discussion",borderwidth = 15, height = 100, width = 100)
ia_frame.pack(fill=BOTH, side=TOP)

user_says = StringVar()
user_text = Label(ia_frame, textvariable=user_says, anchor = NE, justify =   RIGHT, 

bg="white") 
user_text.pack(fill=X) 

ia_says = StringVar()
ia_text = Label(ia_frame, textvariable=ia_says, anchor = NW, justify = LEFT, bg="white") 
ia_text.pack(fill=X) 

user_texts = []
ia_texts = []
user_says_list = []
ia_says_list = []
def Enter_pressed(event):
    """Took the current string in the Entry field."""
    input_get = input_field.get()
    input_user.set("")
    user_says1 = StringVar()
    user_says1.set(input_get + "\n")
    user_text1 = Label(ia_frame, textvariable=user_says1, anchor = NE, justify =   RIGHT, 

bg="white") 
    user_text1.pack(fill=X)
    user_texts.append(user_text1)
    user_says_list.append(user_says1)
    ia_says1 = StringVar()
    ia_says1.set(ia_answers)
    ia_text1 = Label(ia_frame, textvariable=ia_says1, anchor = NW, justify = LEFT, 

bg="white") 
    ia_text1.pack(fill=X)
    ia_texts.append(ia_text1)
    ia_says_list.append(ia_says1)

input_field.bind("<Return>", Enter_pressed)
window.mainloop()