Frame border disappears when focus is lost on window

Question:

I have a tkinter window from which I open another window. This window contains a Frame which is located inside a Canvas. I’m going to make a picture from the window contents but the problem is that the frame border deactivates itself when the focus isn’t on the window or I click the meant button for that and therefore is not present in the picture (this happens only in the real code).

In this example the frame border isn’t visible after the window is created. By clicking the "Save" button which has no function here, it becomes visible. When the focus is lost on this window the frame disappears but is activated again after clicking somewhere in this window.

I have not the slightest idea why the frame border should behave like this at all. Is there a way to constantly display it?

import tkinter as tk
from tkinter import ttk

def open():
    window = tk.Toplevel()

    canvas = tk.Canvas(window, bd=0, highlightthickness=0)
    canvas.grid(row=0, column=0)

    frame = tk.Frame(canvas, bg="red")
    frame.config(highlightthickness=5, highlightcolor="black")
    frame.grid(row=0, column=0)

    text = tk.Label(frame, text="Text")
    text.grid(row=0, column=0)

    save_button = ttk.Button(frame, text="Save")
    save_button.grid(row=1, column=0, columnspan=1, pady=5)

root = tk.Tk()
open_button = ttk.Button(root, command=lambda: [open()], text="Open")
open_button.grid(row=0, column=0)
root.mainloop()
Asked By: Plantz

||

Answers:

The highlightthickness and highlightcolor options are doing exactly what they are designed to do: they only appear when the frame has the focus. It is not designed to be the border of the frame, but rather an indicator of when the frame has focus.

If you want a permanent border then you should use borderwidth and relief options, and/or put your frame inside another frame that uses any color you want for the border color.

Answered By: Bryan Oakley