【问题标题】:tkinter checkbutton different imagetkinter checkbutton 不同的图像
【发布时间】:2020-02-21 21:15:19
【问题描述】:

我想要一个 tkinter 检查按钮,它基本上有我自己的图像用于打开和关闭,所以不是默认的检查按钮。我在互联网上搜索了解决方案,但找不到任何东西。我认为 ttk 样式可能有可能,但我不确定如何

尝试更改 checkbutton 中的 selectimage 选项,但没有任何效果

编辑: 将 indicatoron 设置为 false,然后更改 image 和 selectimage 工作

【问题讨论】:

  • “不是默认的检查按钮”:试试indicatoron=FalseEdit你的问题并展示你的尝试。

标签: python tkinter styles ttk


【解决方案1】:

您需要为未选中状态设置image 选项,为选中状态设置selectimage 选项。您还需要将 indicatoron 设置为 False 以便 tkinter 不显示默认指示器。

这是一个简单的例子:

import tkinter as tk
root = tk.Tk()

on_image = tk.PhotoImage(width=48, height=24)
off_image = tk.PhotoImage(width=48, height=24)
on_image.put(("green",), to=(0, 0, 23,23))
off_image.put(("red",), to=(24, 0, 47, 23))

var1 = tk.IntVar(value=1)
var2 = tk.IntVar(value=0)
cb1 = tk.Checkbutton(root, image=off_image, selectimage=on_image, indicatoron=False,
                     onvalue=1, offvalue=0, variable=var1)
cb2 = tk.Checkbutton(root, image=off_image, selectimage=on_image, indicatoron=False,
                     onvalue=1, offvalue=0, variable=var2)

cb1.pack(padx=20, pady=10)
cb2.pack(padx=20, pady=10)

root.mainloop()

【讨论】:

    【解决方案2】:

    ttk.Checkbutton 小部件的选项比 tk.Checkbutton 小部件少。例如。 selectimageindicatoron 选项不可用。

    为了达到answer by @ByranOakley 中所示的相同结果,执行了以下操作:

    import tkinter as tk
    import tkinter.ttk as ttk
    
    root = tk.Tk()
    root['background'] = 'white'  # for linux distro
    
    # Remove the indicator from a customised TCheckbutton style layout
    s = ttk.Style()
    s.layout('no_indicatoron.TCheckbutton',
             [('Checkbutton.padding', {'sticky': 'nswe', 'children': [
                 # ('Checkbutton.indicator', {'side': 'left', 'sticky': ''}),
                 ('Checkbutton.focus', {'side': 'left', 'sticky': 'w',
                                        'children':
                                        [('Checkbutton.label', {'sticky': 'nswe'})]})]})]
             )
    
    on_image = tk.PhotoImage(width=48, height=24)
    off_image = tk.PhotoImage(width=48, height=24)
    on_image.put(("green",), to=(0, 0, 23, 23))
    off_image.put(("red",), to=(24, 0, 47, 23))
    
    var1 = tk.IntVar(value=1)
    var2 = tk.IntVar(value=0)
    
    
    # Define functions to check Checkbutton state to show the corrsponding image.
    def cb1_state():
        if cb1.instate(['!disabled', 'selected']):
            cb1['image'] = on_image
            print(f'cb1-on_image {var1.get()}')
        else:
            cb1['image'] = off_image
            print(f'cb1-off_image {var2.get()}')
        print()
    
    
    def cb2_state():
        if cb2.instate(['!disabled', 'selected']):
            cb2['image'] = on_image
            print(f'cb2-on_image {var2.get()}')
        else:
            cb2['image'] = off_image
            print(f'cb2-off_image {var2.get()}')
        print()
    
    
    # Use the customised style and functions
    cb1 = ttk.Checkbutton(root, image=off_image, onvalue=1, offvalue=0,
                          variable=var1, style='no_indicatoron.TCheckbutton',
                          command=cb1_state)
    cb2 = ttk.Checkbutton(root, image=off_image, onvalue=1, offvalue=0,
                          variable=var2, style='no_indicatoron.TCheckbutton',
                          command=cb2_state)
    
    cb1.pack(padx=20, pady=10)
    cb2.pack(padx=20, pady=10)
    
    root.mainloop()
    

    【讨论】:

    • 谢谢!这很适合我的用例,只需稍作调整。
    猜你喜欢
    • 1970-01-01
    • 2018-10-20
    • 2018-10-26
    • 1970-01-01
    • 1970-01-01
    • 2020-04-07
    • 2016-06-16
    • 2020-12-05
    • 1970-01-01
    相关资源
    最近更新 更多