CheckboxTreeview() - 复选框的缩放尺寸,改变颜色
CheckboxTreeview() - scale size of checkboxes, change colors
CheckboxTreeview() Python 的 Tkinter 是一个了不起的 class。不幸的是,在 HDPI 显示器(比如我的 4K 电视)上,复选框可能很难看清:
如何在 HDPI 监视器上更容易辨别复选框?可以更改颜色吗?
CheckboxTreeview() 使用 19x14 像素大小的库存 .png
图片,这些图片可能看起来很小。我创建了一个函数来根据字体大小制作可缩放的复选框:
注意我删除了在发现 CheckboxTreeview() 之前使用的旧程序中的背景选择突出显示
代码非常简单,并包含注释以便于使用:
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
补充给出的答案:这将绘制一个实际的“选中”图标而不是矩形。最终尺寸应符合已给出答案的尺寸。
它看起来像这样:
编辑: 但是你当然可以使用任何你喜欢的颜色
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)
CheckboxTreeview() Python 的 Tkinter 是一个了不起的 class。不幸的是,在 HDPI 显示器(比如我的 4K 电视)上,复选框可能很难看清:
如何在 HDPI 监视器上更容易辨别复选框?可以更改颜色吗?
CheckboxTreeview() 使用 19x14 像素大小的库存 .png
图片,这些图片可能看起来很小。我创建了一个函数来根据字体大小制作可缩放的复选框:
注意我删除了在发现 CheckboxTreeview() 之前使用的旧程序中的背景选择突出显示
代码非常简单,并包含注释以便于使用:
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
补充给出的答案:这将绘制一个实际的“选中”图标而不是矩形。最终尺寸应符合已给出答案的尺寸。
它看起来像这样:
编辑: 但是你当然可以使用任何你喜欢的颜色
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)