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()
Answers:
@Idlehands Thanks for citing Fredrik Lundh’s effbot.org explanation on background
. I think I finally understand Fredrik Lundh’s documentation. Namely,
bg=""
is a valid argument that will prevent the updating of the Frame
widget’s background, and
- 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:
- 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.
- 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:
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()
@Idlehands Thanks for citing Fredrik Lundh’s effbot.org explanation on background
. I think I finally understand Fredrik Lundh’s documentation. Namely,
bg=""
is a valid argument that will prevent the updating of theFrame
widget’s background, and- the
background
orbg
of aFrame
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:
- Firstly, the
Frame
widget’s width reduces in size. When this happens, I suspect theTk
widow background colour that filled theFrame
widget is lost. - 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. Becausebg=""
prevents updating, i.e. prevents filling the Frame widget background with a defined background colour, theFrame
widget’s background will default to the “application background colour”. Meaning it will be filled with the exact color of the area directly behind theFrame
widget and theTk
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: