英文:
Chatbot with tkinter: labels not showing in the frame with scrollbar
问题
我正在开发一个使用tkinter创建的简单聊天机器人应用程序。
GUI:
root = Tk()
root.title("Chatbot")
ico = Image.open('../assets/app_icon.png')
photo = ImageTk.PhotoImage(ico)
root.wm_iconphoto(False, photo)
# 窗口尺寸和位置
w_width, w_height = 400, 650
s_width, s_height = root.winfo_screenwidth(), root.winfo_screenheight()
x, y = (s_width / 2) - (w_width / 2), (s_height / 2) - (w_height / 2)
root.geometry('%dx%d+%d+%d' % (w_width, w_height, x, y - 30)) # 居中显示
root.configure(bg=background)
root.pack_propagate(0)
# 框架
root1 = Frame(root, bg=background)
root2 = Frame(root, bg=background)
root3 = Frame(root, bg=background)
for f in (root1, root2, root3):
f.grid(row=0, column=0, sticky='news')
wrapper = Frame(root1, height=550, width=380, background=background)
wrapper.pack(fill="both", expand=True)
canvas = Canvas(wrapper, height=550, width=380, background=background)
canvas.pack(fill="both", expand=True, side="left")
scroll = Scrollbar(wrapper, orient="vertical", command=canvas.yview)
scroll.pack(side="right", fill="y")
canvas.config(yscrollcommand=scroll.set)
canvas.bind('<Configure>', lambda e: canvas.configure(scrollregion=canvas.bbox("all")))
chat_Frame = Frame(canvas)
chat_Frame.bind("<Configure>", lambda e: canvas.config(scrollregion=canvas.bbox("all")))
contentWindow = canvas.create_window((0, 0), window=chat_Frame, anchor="nw")
chat_Frame.pack(padx=10)
chat_Frame.pack_propagate(0)
# 底部灰色框架
bottom_Frame = Frame(root1, bg=bottomFrameColor, height=100)
bottom_Frame.pack(fill=X, side=BOTTOM)
textMode_Frame = Frame(bottom_Frame, bg=bottomFrameColor)
textMode_Frame.pack(fill=BOTH)
textMode_Frame.pack_forget()
# 按钮
...
raiseFrame(root1)
root.mainloop()
由于这是一个聊天机器人,一旦我输入请求,它会调用附加功能,然后在机器人生成回答并将消息附加到chat_Frame中:
def attachTOframe(text, bot=False):
if bot:
Label(chat_Frame, image=botIcon, bg=background).pack(anchor='w', pady=0)
botchat = customtkinter.CTkLabel(chat_Frame, text=text, fg_color=botChatTextFg,
text_color=textColorDark, corner_radius=15, padx=3, pady=3, justify=LEFT,
font=('Montserrat', 12, 'bold'))
botchat.pack(anchor='w', ipadx=2, ipady=2, pady=5)
else:
Label(chat_Frame, image=userIcon, bg=background).pack(anchor='e', pady=0)
userchat = customtkinter.CTkLabel(chat_Frame, text=text, fg_color=userChatTextFg,
text_color=textColorDark, corner_radius=15, padx=3, pady=3, justify=RIGHT,
font=('Montserrat', 12, 'bold'))
userchat.pack(anchor='e', ipadx=2, ipady=2, pady=5)
然而,没有任何消息附加到框架,并且我也没有收到错误消息,有人能提供一些可能出错的有用提示吗?没有滚动条时它正常工作。
英文:
I am working on simple chatbot app with GUI in tkinter.
GUI:
root = Tk()
root.title("Chatbot")
ico = Image.open('../assets/app_icon.png')
photo = ImageTk.PhotoImage(ico)
root.wm_iconphoto(False, photo)
#Window dimensions and position
w_width, w_height = 400, 650
s_width, s_height = root.winfo_screenwidth(), root.winfo_screenheight()
x, y = (s_width / 2) - (w_width / 2), (s_height / 2) - (w_height / 2)
root.geometry('%dx%d+%d+%d' % (w_width, w_height, x, y - 30)) #center location of the screen
root.configure(bg = background)
root.pack_propagate(0)
#Frames
root1 = Frame(root, bg = background)
root2 = Frame(root, bg = background)
root3 = Frame(root, bg = background)
for f in (root1, root2, root3):
f.grid(row = 0, column = 0, sticky = 'news')
wrapper = Frame(root1, height = 550, width = 380, background = background)
wrapper.pack(fill = "both", expand = True)
canvas = Canvas(wrapper, height = 550, width = 380, background = background)
canvas.pack(fill = "both", expand = True, side = "left")
scroll = Scrollbar(wrapper, orient="vertical", command = canvas.yview)
scroll.pack(side = "right", fill = "y")
canvas.config(yscrollcommand = scroll.set)
canvas.bind('<Configure>', lambda e: canvas.configure(scrollregion = canvas.bbox("all")))
chat_Frame = Frame(canvas)
chat_Frame.bind("<Configure>", lambda e: canvas.config(scrollregion = canvas.bbox("all")))
contentWindow = canvas.create_window((0,0), window = chat_Frame, anchor = "nw")
chat_Frame.pack(padx = 10)
chat_Frame.pack_propagate(0)
#Bottom Gray Frame
bottom_Frame = Frame(root1, bg = bottomFrameColor, height = 100)
bottom_Frame.pack(fill = X, side = BOTTOM)
textMode_Frame = Frame(bottom_Frame, bg = bottomFrameColor)
textMode_Frame.pack(fill = BOTH)
textMode_Frame.pack_forget()
#Buttons
...
raiseFrame(root1)
root.mainloop()
Since it is a chatbot, there is additional function that is called once I enter request as well as after bot generates answer and it attaches messages to the chat_Frame:
def attachTOframe(text, bot = False):
if bot:
Label(chat_Frame, image=botIcon, bg=background).pack(anchor='w', pady=0)
botchat = customtkinter.CTkLabel(chat_Frame, text = text, fg_color = botChatTextFg,
text_color = textColorDark, corner_radius = 15, padx = 3, pady = 3, justify = LEFT,
font=('Montserrat', 12, 'bold'))
botchat.pack(anchor = 'w',ipadx = 2, ipady = 2, pady = 5)
else:
Label(chat_Frame, image=userIcon, bg=background).pack(anchor='e', pady=0)
userchat = customtkinter.CTkLabel(chat_Frame, text = text, fg_color = userChatTextFg,
text_color = textColorDark, corner_radius = 15, padx = 3, pady = 3, justify = RIGHT,
font=('Montserrat', 12, 'bold'))
userchat.pack(anchor = 'e', ipadx = 2, ipady = 2, pady = 5)
However, none of the messages are attached to the frame and I also get no errors, could someone provide some useful tips what might be wrong? It worked without scrollbar.
答案1
得分: 1
你可以使用 create_window
将窗口添加到画布上,但是接下来一行调用 chat_Frame.pack(padx = 10)
取消了这个操作。你需要移除那行代码,否则框架不会在画布内滚动,因为它不再由画布管理。
英文:
You add the window to the canvas with create_window
, but you undo that by calling chat_Frame.pack(padx = 10)
on the next line. You need to remove that line, otherwise the frame will not scroll within the canvas because it is no longer being managed by the canvas.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论