Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Frame border disappears when focus is lost on window

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?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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()

>Solution :

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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading