1

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.

Milos
  • 35
  • 6

1 Answers1

1

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.

Bryan Oakley
  • 370,779
  • 53
  • 539
  • 685