【问题标题】:CheckboxTreeview() - scale size of checkboxes, change colorsCheckboxTreeview() - 复选框的缩放大小,更改颜色
【发布时间】:2023-03-23 07:47:01
【问题描述】:

CheckboxTreeview() for Python 的 Tkinter 是一个了不起的类。不幸的是,在 HDPI 显示器(比如我的 4K 电视)上,该复选框很难阅读:

如何在 HDPI 显示器上更容易识别复选框?可以换颜色吗?

【问题讨论】:

    标签: python tkinter checkbox treeview


    【解决方案1】:

    CheckboxTreeview() 使用 19x14 像素大小的库存 .png 图像,可能看起来很小。我创建了一个函数来根据字体大小制作可缩放的复选框:

    注意我删除了在发现 CheckboxTreeview() 之前使用的旧程序中的背景选择突出显示

    代码非常简单,并且包含 cmets 以方便使用:

    def make_checkboxes(hgt, outc, fillc, chkc):
    
        ''' Create CheckboxTreeview(); 'unchecked', 'checked' and 'tristate'
            Parms: height, outline color, fill color, checkmark color
    
            Returns list of three PhotoImages for parent. How to use 4K screen:
    
                MON_FONTSIZE = 12
                row_height=int(MON_FONTSIZE*2.2)
                style.configure("Treeview", font=(None, MON_FONTSIZE), \
                                rowheight=row_height)
    
                self.checkboxes = make_checkboxes(row_height-6, 'black', \
                                                  'white', 'deepskyblue')
                self.cd_tree.tag_configure("unchecked", image=self.checkboxes[0])
                self.cd_tree.tag_configure("tristate", image=self.checkboxes[1])
                self.cd_tree.tag_configure("checked", image=self.checkboxes[2])
    
        '''
        from PIL import Image, ImageTk, ImageDraw       # Pillow image processing
    
        if hgt % 2 == 1: hgt = hgt + 1                  # even number box height
        if hgt < 14: hgt = 14                           # minimum box height
        wid = hgt                                       # square: width = heidht
        wid_pad = int(wid * 10 / 20)                    # lead + trailing padding
        xo = int(wid_pad / 5)                           # x-offset after lead pad
        retn_images = []                                # list of three images
    
        image = Image.new("RGBA", (wid + wid_pad, hgt), (0, 0, 0, 0))
        draw = ImageDraw.Draw(image)                    # Create drawable image
    
        draw.rectangle([(xo, 0), (xo + wid, hgt-1)], fill=fillc, outline=outc)
        retn_images.append(ImageTk.PhotoImage(image))   # Unchecked
        draw.rectangle([(xo+3, 7), (xo + wid-3, hgt-8)], fill=chkc)
        retn_images.append(ImageTk.PhotoImage(image))   # Tristate
        draw.rectangle([(xo+3, 3), (xo + hgt-3, hgt-4)], fill=chkc)
        retn_images.append(ImageTk.PhotoImage(image))   # Checked
    
        return retn_images
    

    【讨论】:

    • 很好,我将其扩展了一点,以绘制类似于真正的选中图标的东西。但它不适合评论,所以我将其发布为新的分析器
    【解决方案2】:

    补充给定的答案:这将绘制一个实际的“选中”图标而不是矩形。最终尺寸应符合已给出答案的尺寸。

    它看起来像这样:

    编辑:但你当然可以使用任何你喜欢的颜色

    scalingFactor = 10
    
    imageChecked = Image.new("RGBA", ((wid + wid_pad) *scalingFactor, hgt *10), (0, 0, 0, 0))
    drawChecked = ImageDraw.Draw(imageChecked) 
    drawChecked.rectangle([(xo*scalingFactor, 0), ((xo + wid)*scalingFactor, (hgt-1)*scalingFactor)], fill=fillc, outline=outc)
    
    startPoint = (xo*scalingFactor+int(0.15*wid*scalingFactor), (0.6*hgt*scalingFactor))
    middlePoint = (xo*scalingFactor+int(0.5*wid*scalingFactor), (int(0.8*hgt*scalingFactor)))
    endPoint =(xo*scalingFactor+int(0.85*wid*scalingFactor), (int(0.1*hgt*scalingFactor)))
    
    drawChecked.line([startPoint,middlePoint,endPoint], fill=chkc, width=int(hgt*scalingFactor/5), joint="curve")
    
    imageChecked = imageChecked.resize((wid+wid_pad, hgt), Image.ANTIALIAS)
    

    【讨论】:

    • 不错。但我认为 Google 会使用带有白色 cueckmark 的蓝色背景。
    • @WinEunuuchs2Unix 当然,你可以使用任何你想要的颜色。我猜这就是你原来帖子的全部想法:)
    猜你喜欢
    • 2021-11-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-12-06
    • 2015-11-17
    • 1970-01-01
    • 1970-01-01
    • 2020-09-21
    相关资源
    最近更新 更多