为什么 tkinter 不抛出/引发异常? [英] Why tkinter don't throw/raise an exception?

查看:44
本文介绍了为什么 tkinter 不抛出/引发异常?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问题是,如果您运行下面的代码并调整窗口大小,您将得到意料之外的行为.小部件没有按照应有的方式呈现,背景也没有有一些奇怪的颜色.

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.

我知道如果你设置了一个参数,那么你应该用一个valide值填充它.如果你像我一样用空字符串填充它它不会抛出 <_tkinter.TclError: unknown color name ""> .

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 "">.

我的问题是为什么我们没有错误?那是 tkinter 错误吗?因为如果你设置了 Canvas 的 bg 参数作为一个空字符串,你会得到一个错误:未知颜色名称"

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 感谢您在 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.

这篇关于为什么 tkinter 不抛出/引发异常?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆