Tkinter制作的聊天机器人:标签不显示在带有滚动条的框架中。

huangapple go评论65阅读模式
英文:

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(&quot;Chatbot&quot;)
ico = Image.open(&#39;../assets/app_icon.png&#39;)
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(&#39;%dx%d+%d+%d&#39; % (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 = &#39;news&#39;)	
wrapper = Frame(root1, height = 550, width = 380, background = background)
wrapper.pack(fill = &quot;both&quot;, expand = True)
canvas = Canvas(wrapper, height = 550, width = 380, background = background)
canvas.pack(fill = &quot;both&quot;, expand = True, side = &quot;left&quot;)
scroll = Scrollbar(wrapper, orient=&quot;vertical&quot;, command = canvas.yview)
scroll.pack(side = &quot;right&quot;, fill = &quot;y&quot;)
canvas.config(yscrollcommand = scroll.set)
canvas.bind(&#39;&lt;Configure&gt;&#39;, lambda e: canvas.configure(scrollregion = canvas.bbox(&quot;all&quot;)))
chat_Frame = Frame(canvas)
chat_Frame.bind(&quot;&lt;Configure&gt;&quot;, lambda e: canvas.config(scrollregion = canvas.bbox(&quot;all&quot;)))
contentWindow = canvas.create_window((0,0), window = chat_Frame, anchor = &quot;nw&quot;)
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=&#39;w&#39;, 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=(&#39;Montserrat&#39;, 12, &#39;bold&#39;))
botchat.pack(anchor = &#39;w&#39;,ipadx = 2, ipady = 2, pady = 5)
else:
Label(chat_Frame, image=userIcon, bg=background).pack(anchor=&#39;e&#39;, 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=(&#39;Montserrat&#39;, 12, &#39;bold&#39;))
userchat.pack(anchor = &#39;e&#39;, 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.

huangapple
  • 本文由 发表于 2023年7月31日 19:50:51
  • 转载请务必保留本文链接:https://go.coder-hub.com/76803331.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定