且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

为什么 tkinter 不抛出/引发异常?

更新时间:2022-06-24 03:08:01

@Idlehands 感谢您在 background 中引用 Fredrik Lundh 的 effbot.org 解释.我想我终于明白了 Fredrik Lundh 的文档.也就是说,

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

  1. bg="" 是一个有效的参数,它将阻止更新 Frame 小部件的背景,并且
  2. Frame 小部件的 backgroundbg 默认为应用程序背景色.
  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.

因此,当调整 Tk 窗口的大小时,例如当 Tk 窗口的右边缘向左移动到达 Canvas 小部件的右边缘,然后向右移动回到其原始位置时,会发生以下现象:

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. 首先,Frame 小部件的宽度减小.发生这种情况时,我怀疑填充 Frame 小部件的 Tk 寡妇背景颜色丢失了.
  2. 其次,当 Frame 小部件的宽度下一次返回其原始大小时,需要使用定义的颜色更新(即填充)其背景.由于 bg="" 阻止更新,即阻止使用定义的背景颜色填充 Frame 小部件背景,Frame 小部件的背景将默认为应用程序背景颜色".这意味着它将用 Frame 小部件和 Tk 窗口正后方区域的确切颜色填充,就好像它变得透明"一样.
  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'.

回答@Turjak_art 问题,tkinter 没有产生错误或异常,因为 bg=""Frame 小部件的有效参数.

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

显示快速调整大小的框架背景的图像.

Image showing Frame background with fast resizing.

图像显示了缓慢调整大小的框架背景.

Image showing Frame background with slow resizing.