Search code examples
tkintercolorsbordertkinter-scrolledtext

Using tkinter and scrolledText, I would like to create a pop-up window with red borders


I would like a red border on the pop-up window. The border width works, but not the color. Border color continues to be black, not red. I tried two different ways. Both present black borders.

Method #1 (Preferred)

import tkinter as tk
from tkinter.scrolledtext import ScrolledText


                def display_dataframe():

                    # Create a tkinter window
                    window = tk.Tk()
                    window.title('Stock Alert')

                    # Create a scrolled text widget to display the DataFrame

                    text_widget = ScrolledText(window, width=100, height=15, borderwidth=5, relief="solid", highlightbackground="red")
                    text_widget.insert(tk.END, alert_list_display.to_string(index=False))  # Insert DataFrame content
                    text_widget.pack()

                    # Start the tkinter main loop
                    window.mainloop()

                # Call the function to display the DataFrame in a pop-up window
                display_dataframe()

Method #2

import tkinter as tk
from tkinter import scrolledtext

def change_border_color(frame, color):
    frame.configure(highlightbackground=color)

# Create a tkinter window
window = tk.Tk()
window.title('ScrolledText Border Color')

# Create a custom frame with a ScrolledText widget
frame = tk.Frame(window, borderwidth=10, relief="solid", highlightbackground="red")
frame.pack()

text_widget = scrolledtext.ScrolledText(frame, width=100, height=15)
text_widget.pack(fill="both", expand=True)

# Change the border color of the custom frame
change_border_color(frame, "blue")

# Start the tkinter main loop
window.mainloop()

Solution

  • The simplest solution is to color the parent of the scrolledtext, and use padding to make the area around the scrolledtext visible.

    screenshot