`import customtkinter as ctk
from info import IP, PORT, logo_path
from PIL import Image, ImageTk
from datetime import datetime
class Menu(ctk.CTk):
def init(self):
super().init()
self.title(“Y – Artificial Intelligence”)
self.resizable(False, False)
self.configure_window()
self._set_appearance_mode(“dark”)
self.wm_iconbitmap(logo_path)
self.SendMessageEnabled = True
self.username = “”
self.age = 0
self.openai_api_key = “”
def UpdateInfo(self, username, age, openai_api_key):
self.username = username
self.age = age
self.openai_api_key = openai_api_key
print(f"Username: {self.username}, Age: {self.age}, OpenAI API Key: {self.openai_api_key}")
def configure_window(self):
# Get the screen width and height
screen_width = self.winfo_screenwidth()
screen_height = self.winfo_screenheight()
# Set the geometry of the window to fullscreen
self.geometry("{0}x{1}+0+0".format(screen_width, screen_height))
self.create_text_box()
def create_text_box(self):
# Create a custom font
custom_font = ctk.CTkFont(family="Century Gothic", size=16, weight="bold")
# Create the text box
self.text_box = ctk.CTkTextbox(
self,
border_color="#818181",
border_width=1.5,
font=custom_font,
fg_color="#242525",
border_spacing=5,
width=1200,
height=10, # Adjust height as needed
wrap="char", # Wrap text at word boundaries
scrollbar_button_color="#000000",
)
self.text_box.pack(padx=90, pady=(0, 20), side="bottom", anchor="se")
self.text_box.insert("1.0", "Message Y...") # Insert placeholder text
self.text_box.configure(text_color="#818181") # Change text color to placeholder color
self.text_box.bind("<FocusIn>", self.on_textbox_focus)
self.text_box.bind("<Key>", self.on_key_pressed)
self.text_box.bind("<Return>", self.on_enter_pressed) # Bind Enter key event
# Create the scrollable frame
self.scroll_frame = ctk.CTkScrollableFrame(
self,
scrollbar_button_color="black", # Set the scrollbar color to black
width=1180, # Adjust width as needed
height=1000, # Adjust height as needed
fg_color="transparent" # Set background color to transparent
)
self.scroll_frame.pack(padx=(250,80), pady=(10, 10))
self.username="Y - AI"
self.put_message(f'Hello, {self.username}! I am Y, your personal AI assistant. How can I assist you today?')
def on_textbox_focus(self, event):
# Check if textbox is empty
if self.text_box.get("1.0", "end-1c") == "":
self.text_box.insert("1.0", "Message Y...") # Insert placeholder text
self.text_box.configure(text_color="#818181") # Change text color to placeholder color
def on_key_pressed(self, event):
# Check if the placeholder text is present when a key is pressed
if self.text_box.get("1.0", "end-1c") == "Message Y...":
self.text_box.delete("1.0", "end") # Remove placeholder text
self.text_box.configure(text_color="white") # Change text color to white
# Get the current text in the text box
text = self.text_box.get("1.0", "end-1c")
# Calculate the number of lines based on the number of characters
num_chars = len(text)
line_height = 25 # Adjust this value as needed
max_char_per_line = 111 # Maximum characters per line
# Manual switch statements to adjust the number of lines
if num_chars <= max_char_per_line:
num_lines = 1
elif max_char_per_line < num_chars <= max_char_per_line*2:
num_lines = 2
elif max_char_per_line*2 < num_chars <= max_char_per_line*3:
num_lines = 3
elif max_char_per_line*3 < num_chars <= max_char_per_line*4:
num_lines = 4
else:
num_lines = 5
# Calculate the new height of the text box based on the number of lines
max_height = 20 # Maximum number of lines
new_height = min(num_lines, max_height) * line_height
# Adjust the height of the text box
self.text_box.configure(height=new_height)
# Eneter pressed
def on_enter_pressed(self, event):
if self.SendMessageEnabled:
self.SendMessageEnabled = True # Change Value
# Print the text when Enter key is pressed
text = self.text_box.get("1.0", "end-1c")
self.text_box.delete(0.0, "end")
self.text_box.mark_set("insert", "1.0")
self.text_box.configure(height=20)
self.put_message(text)
self.scroll_frame.set(1.0,1.0)
def put_message(self, message):
# Get the current time
current_time = datetime.now().strftime("%H:%M:%S")
# Format the message to display including username and timestamp
message_text = f"{self.username} {' ' * (20 - len(self.username))} {current_time}n"
# Limit the length of each line of the message
max_line_length = 60
for i in range(0, len(message), max_line_length):
message_text += message[i:i+max_line_length] + "n"
# Create the message label inside the scrollable frame's inner frame
message_label = ctk.CTkLabel(
self.scroll_frame,
text=message_text,
fg_color="transparent",
font=ctk.CTkFont(family="Century Gothic", size=16),
justify="left"
)
message_label.pack(pady=(0, 5), side="top", anchor="sw")`
In on_enter_pressed, I want the frame to scroll to the end. How do I do that?
I tried to use self.scroll_frame._scrollbar.set(1.0, 1.0), but it didnt work.
Movies Trailers is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
1