Aug-04-2023, 01:33 PM
Hi everyone, I'm trying to create a chatbot, the chatbot is ok but I'm struggling with the scrollbar, when I run my code everything is ok except the scrollbar, it is there, the scrollbar moves, but the chat doesn't move and it's impossible to see the scrolling chat. (I'm a beginner in coding). Could you please help me with how to make this scrollbar move the chat please ? :) Here is the code I'm using :
from tkinter import * import datetime from tkinter import Scrollbar root = tk.Tk() root.geometry("600x400") # Set the initial size of the window text_widget = Text(root) scrollbar = Scrollbar(root) scroll.pack(side=RIGHT) text_widget.configure(yscrollcommand=scrollbar.set) scrollbar.config(command=text_widget.yview) scrollbar.grid(row=0, column=1, sticky='ns') text_widget.pack(side=RIGHT, fill=BOTH, expand=True) text_widget.bind("<MouseWheel>", lambda event: text_widget.yview_scroll(-1 * int((event.delta / 120)), "units")) # Custom widget for speech bubble class SpeechBubble(Frame): def __init__(self, master, message, is_client=True): super().__init__(master) self.is_client = is_client self.message = message self.create_widgets() def create_widgets(self): if self.is_client: bg_color = "#DCF8C6" # Client's message bubble color text_color = "black" align = "right" # Align client's bubble to the right padx = (50, 10) # Add some horizontal padding to the client's bubble pady = (5, 0) # Add some vertical padding to the client's bubble else: bg_color = "#F8F8F8" # king's message bubble color text_color = "black" align = "left" # Align king's bubble to the left padx = (10, 50) # Add some horizontal padding to king's bubble pady = (0, 5) # Add some vertical padding to king's bubble bubble_frame = Frame(self, bg=bg_color, padx=10, pady=5, borderwidth=2, relief="solid") bubble_frame.pack(side=align, fill="x", padx=padx, pady=pady) # Use side=align to align the bubble to the left or right bubble_label = Label(bubble_frame, text=self.message, wraplength=300, bg=bg_color, fg=text_color, justify="left", font=("Arial", 12)) bubble_label.pack() # Define who speaks def envoie(): message = e.get() message_with_prefix = "Me: " + message if txt.index("end-1c") != "1.0": # Check if there is content in the text widget (excluding the trailing newline) txt.insert(END, "\n") # Insert a newline to separate messages message_frame = Frame(txt) message_frame.pack(anchor="e" if txt.index("end-1c") == "1.0" else "w") # Align the message frame to the right if it's the first message, otherwise align to the left speech_bubble = SpeechBubble(message_frame, message_with_prefix, is_client=True) speech_bubble.pack(side="right") # Align the client's bubble to the right e.delete(0, END) text_widget.yview() if 'Hello' in message: response = "Hello" else: response = "I'm sorry, I don't understand that." response_frame = Frame(txt) response_frame.pack(anchor="w") # Align the response frame to the left response_bubble = SpeechBubble(response_frame, "king: " + response, is_client=False) response_bubble.pack(side="left") # Align king's bubble to the left # Scroll to the bottom of the text widget to show the latest message txt.see(END) text_widget.insert(END, message_with_prefix) # Define where the text goes: txt = Text(root, font=("Arial", 12), wrap="word", padx=10, pady=10) txt.grid(row=0, column=0, sticky="nsew") # Use sticky="nsew" to make the widget expand in all directions e = Entry(root, width=60) e.grid(row=2, column=0, padx=10, pady=10) # Bind the "Enter" key to the function envoie() e.bind("<Return>", lambda event: envoie()) # Define the "enter" button: envoyer = Button(root, text="Enter", command=envoie) envoyer.grid(row=2, column=1, padx=10, pady=10) # Make the rows and columns of the root grid expand to fill the available space root.grid_rowconfigure(0, weight=1) root.grid_columnconfigure(0, weight=1) root.title("king") root.mainloop()