Tkinter 如何更改组合框小部件箭头图像 [英] Tkinter how to change combobox widget arrow image

查看:32
本文介绍了Tkinter 如何更改组合框小部件箭头图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试更改组合框小部件中的箭头图像.我知道您需要创建一个新元素并为其设置样式.我一直在寻找有关如何执行此操作的信息,但似乎在网上找不到任何内容.在下面的代码中,我创建了图像并将其显示在标签中,但我希望该图像作为组合框箭头图像.

I'm trying to change the arrow image in a combobox widget. I understand that you need to create a new element and style it. I have been looking for info on how to do this and can't seem to find anything online. In the code below I create the image and display it in a label, but I want that image as the combobox arrow image.

from tkinter import *
from tkinter import ttk

root_window = Tk()
style = ttk.Style()

photo = """
R0lGODlhDQAQAPQAAFw1Zl42Z2A6amE6amI7a2I8a2I8bKeHqK6BqbSLsLWLsLuV
t7ufu7yfu72hvL6ivb6ivr+jvsKgvsWlwseow8iqxcmrxs+1zNC2zdO70dS90tW9
0tW+0wAAAAAAAAAAACH5BAEAAB0ALAAAAAANABAAAAVMYCd2QFmOKJltGZCOwMZt
7kvKtH3P9RvzutgmZ4tdiL6NBUkyGTaSjMHkEjgyGcuiwnIIRoWIJUG2eFPhCYJy
fhUkmLbNcPjqRL1RCAA7
"""

photo = PhotoImage(data=photo)
l = ttk.Label(root_window, image=photo).grid()

style.layout(
    'Mystyle.TCombobox', [(
        'Combobox.field', {
            'sticky': NSEW,
            'children': [(
                'Combobox.downarrow', {
                    'side': 'right',
                    'sticky': NS
                }
            ), (
                'Combobox.padding', {
                    'expand': '1',
                    'sticky': NSEW,
                    'children': [(
                        'Combobox.textarea', {
                            'sticky': NSEW
                        }
                    )]
                }
            )]
        }
    )]
)

style.element_create('Mystyle.TCombobox.downarrow', 'image', photo)

cbo = ttk.Combobox(root_window, values=('one', 'two', 'three'), style='Mystyle.TCombobox')
cbo.grid()

root_window.mainloop()

推荐答案

你已经很接近了,你只需要在你的自定义样式中使用你的元素.请注意以下示例中如何将 Mystyle.TCombobox.downarrow 用作组合框中的子项之一:

You're very close, you just need to use your element in your custom style. Notice in the following example how Mystyle.TCombobox.downarrow is used as one of the children in the combobox:

style.element_create('Mystyle.TCombobox.downarrow', 'image', photo)
style.layout(
    'Mystyle.TCombobox', [(
        'Combobox.field', {
            'sticky': NSEW,
            'children': [(
                'Mystyle.TCombobox.downarrow', {
                    'side': 'right',
                    'sticky': NS
                }
            ), (
                'Combobox.padding', {
                    'expand': '1',
                    'sticky': NSEW,
                    'children': [(
                        'Combobox.textarea', {
                            'sticky': NSEW
                        }
                    )]
                }
            )]
        }
    )]
)

这篇关于Tkinter 如何更改组合框小部件箭头图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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