Why tkinter don't throw/raise an exception?

Question:

The problem is if you run this code below and resize the window then you will get
unexpected behavior. Widgets are not rendered like they should and the backgrounds
have some wierd colors.

I know if you set a argument then you should
fill it with a valide value. If you do fill it with an empty string like I did
it don’t throw a <_tkinter.TclError: unknown color name “”>.

My Question is why we
don’t get an error? Is that a tkinter bug? Because if you set the bg argument of Canvas
as an empty string you will get an error: unknown color name “”

from tkinter import Tk, Frame, Button, Entry, Canvas

class Gui(Frame):
    def __init__(self, master):
        self.master = master
        self.font1 = font=("Segoe UI",18,"bold")
        self.font2 = font=("Segoe UI",28,"bold")
        Frame.__init__(self, self.master, bg="")
        self.grid()  
        self.create_widgets()

    def create_widgets(self):   
        self.frame_container = Frame(self, bg="")       
        self.frame_container.grid(row=0, column=0)
        self.button_test_1 = Button(self, text="Test1", bg="yellow")
        self.button_test_1.grid()
        self.can = Canvas(self.frame_container, bg="orchid1")
        self.can.grid(row=0, column=0)
        self.entry_test = Entry(self.frame_container)
        self.entry_test.grid(row=0, column=1)            

if __name__ == "__main__":
    root = Tk()
    bug_gui = Gui(root)
    root.mainloop()
Asked By: Module_art

||

Answers:

@Idlehands Thanks for citing Fredrik Lundh’s effbot.org explanation on background. I think I finally understand Fredrik Lundh’s documentation. Namely,

  1. bg="" is a valid argument that will prevent the updating of the Frame widget’s background, and
  2. the background or bg of a Frame widget defaults to the application background color.

Hence, when the Tk window is resized, e.g. when the right edge of the Tk window is move to the left to reach the Canvas widget’s right edge and followed by moving right back to its original position, the following phenomenon happens:

  1. Firstly, the Frame widget’s width reduces in size. When this happens, I suspect the Tk widow background colour that filled the Frame widget is lost.
  2. Secondly, when the Frame widget’s width next returns to its original size, it’s background needs to be updated (i.e. filled) with a defined colour. Because bg="" prevents updating, i.e. prevents filling the Frame widget background with a defined background colour, the Frame widget’s background will default to the “application background colour”. Meaning it will be filled with the exact color of the area directly behind the Frame widget and the Tk window, as if it became ‘transparent’.

Answering @Turjak_art question, tkinter did not yield an Error or Exception because bg="" is a valid argument of the Frame widget.

Edit:

Image showing Frame background with fast resizing.
bg="" fast resizing

Image showing Frame background with slow resizing.
bg="" slow resizing

Answered By: Sun Bear
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.