如何在 tkinter.ttk.Button 中创建箭头并控制其大小?

2023-12-26

我想创建带有箭头的 ttk.button 并更改箭头大小。

我发现“TButton”本质上包含 StyleNamesTButton.leftarrow它不是由 ttk.Style().layout() 公开的。

问题:(1) 如何激活这些 StyleName? (2)如何控制大小.leftarrow?我注意到它有一个arrowsize选项 。我该如何使用它?

import tkinter as tk
import tkinter.ttk as ttk

class App(ttk.Frame):

        def __init__(self, parent):
            ttk.Frame.__init__(self, parent)
            self.parent = parent
            self.setStyle()
            self.setWidget()

        def setStyle(self):
            style = ttk.Style()
            print('Left.TButton layout are:', style.layout('Left.TButton'))
            print("Left.TButton.leftarrow style.element_options: ",
                  style.element_options('Left.TButton.leftarrow'))

            style.configure('Left.TButton', background='yellow')
            style.configure('Left.TButton.leftarrow', background='white',
                            arrowsize=20)

        def setWidget(self):
            self.lbutton = ttk.Button(self.parent, style='Left.TButton')
            self.lbutton2 = ttk.Button(self.parent, style='Left.TButton.leftarrow')
            self.lbutton.pack()
            self.lbutton2.pack()


    if __name__ == '__main__':
        root = tk.Tk()
        root.title('Test')
        root.geometry('200x50')
        app = App(root)
        app.pack(expand=1, fill='both')

经过大量尝试和对 ttk 文档的仔细研究,我发现了以下内容:

  1. 要在按钮中创建箭头,我必须声明一个arrow元素 作为 的孩子focus自定义样式布局中的元素 即用于ttk.Button()小部件。为此,我需要 使用ttk.Style().layout() method.
  2. 箭头的大小取决于标签的字体大小 元素。因此,一个label元素必须在布局中声明 风格的TButton. The arrowsize的选项arrowleft元素似乎不起作用。我已经注释掉了这行不起作用的代码。但是,那arrowcolor的选项leftarrow元素确实有效。要调整label元素的字体大小,ttk.Style().configuration使用了方法。

测试脚本中的方法 2 演示了我的问题的解决方案。

测试代码:

import tkinter as tk
import tkinter.ttk as ttk


class App(ttk.Frame):

    def __init__(self, parent):
        ttk.Frame.__init__(self, parent)
        self.parent = parent
        self.setStyle()
        self.setWidget()

    def setStyle(self):
        style = ttk.Style()
        print('\nDefault TButton layout:')
        print(style.layout('TButton'))

        print ('\nTButton Elements and their options:')
        print("border options: ", style.element_options('Button.border'))
        print("focus options: ",  style.element_options('Button.focus'))
        print("padding options: ",style.element_options('Button.padding'))
        print("label options: ",  style.element_options('Button.label'))
        print("arrow options: ",  style.element_options('Button.arrow'))

        print ('\nElement TButton.label and its options:')
        print("compound: ",  style.lookup('Button.label', 'compound'))
        print("space: ",     style.lookup('Button.label', 'space'))
        print("text: ",      style.lookup('Button.label', 'text'))
        print("font: ",      style.lookup('Button.label', 'font'))
        print("foreground: ",style.lookup('Button.label', 'foreground'))
        print("underline: ", style.lookup('Button.label', 'underline'))
        print("width: ",     style.lookup('Button.label', 'width'))
        print("anchor: ",    style.lookup('Button.label', 'anchor'))
        print("justify: ",   style.lookup('Button.label', 'justify'))
        print("wraplength: ",style.lookup('Button.label', 'wraplength'))
        print("embossed: ",  style.lookup('Button.label', 'embossed'))
        print("image: ",     style.lookup('Button.label', 'image'))
        print("stipple: ",   style.lookup('Button.label', 'stipple'))
        print("background: ",style.lookup('Button.label', 'background'))

        print ('\nElement TButton.arrow and its options:')
        print("background: ", style.lookup('Button.arrow', 'background'))
        print("relief: ",     style.lookup('Button.arrow', 'relief'))
        print("borderwidth: ",style.lookup('Button.arrow', 'borderwidth'))
        print("arrowcolor: ", style.lookup('Button.arrow', 'arrowcolor'))
        print("arrowsize: ",  style.lookup('Button.arrow', 'arrowsize'))

        #Define style Default.TButton with yellow background
        style.configure('Default.TButton', background='yellow')
        #Change the 2 options of the "label" element in its style's layout  
        style.configure('Default.TButton.label', foreground='red')
        style.configure('Default.TButton.label', borderwidth=20)
        print ('\nElement Default.TButton.label and its options (after configuration):')
        print("background: ",  style.lookup('Default.TButton.border', 'background'))
        print("borderwidth: ", style.lookup('Default.TButton.border', 'borderwidth'))

        #Approach 1
        #==========
        # Define style Left.TButton to show the following elements: leftarrow,
        #  padding, label 
        style.layout(
            'Left1.TButton',[
                ('Button.focus', {'children': [
                    ('Button.leftarrow', None),
                    ('Button.padding', {'sticky': 'nswe', 'children': [
                        ('Button.label', {'sticky': 'nswe'}
                         )]}
                     )]}
                 )]
            )
        #Change 3 options of the "arrow" element in style "Left.TButton"
        style.configure('Left1.TButton.leftarrow',
                        background='white',
                        borderwidth=10,
                        arrowsize=20)
        print('\nElement TButton.arrow and its options (after changing):')
        print("background: ",  style.lookup('Left1.TButton.arrow','background'))
        print("borderwidth: ", style.lookup('Left1.TButton.arrow','borderwidth'))
        print("arrowsize: ",   style.lookup('Left1.TButton.arrow','arrowsize'))

        #Approach 2
        #==========
        style.layout(
            'Left2.TButton',[
                ('Button.focus', {'children': [
                    ('Button.leftarrow', None),
                    ('Button.padding', {'sticky': 'nswe', 'children': [
                        ('Button.label', {'sticky': 'nswe'}
                         )]}
                     )]}
                 )]
            )

        style.configure('Left2.TButton',font=('','20','bold'), width=1, arrowcolor='white')
        #style.configure('Left2.TButton', width=1, arrowcolor='white', arrowsize='20')
        #option arrowsize does not work


    def setWidget(self):
        self.lbutton = ttk.Button(self.parent, style='Default.TButton',
                                  text='Default.TButton')
        self.lbutton1 = ttk.Button(self.parent, style='Left1.TButton',
                                   text='Left1.Button')
        self.lbutton2 = ttk.Button(self.parent, style='Left2.TButton',
                                   text='')
        self.lbutton.pack()
        self.lbutton1.pack()
        self.lbutton2.pack()


if __name__ == '__main__':
    root = tk.Tk()
    root.title('Test')
    root.geometry('200x100')
    app = App(root)
    app.pack(expand=1, fill='both')
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何在 tkinter.ttk.Button 中创建箭头并控制其大小? 的相关文章

随机推荐

  • 如何更改“DivideByZeroException”的抛出处理?

    我想给int类似的行为float 即使其能够除以 0but我希望它返回0 此外 我想超载 运算符为int返回一个浮点数 我知道该怎么做 但只是为了完整性而提到 我不想检查每个除法的分母是否为 0 我也宁愿不等待抛出异常 因为异常速度很慢 理
  • 彩色框中的文本

    我正在创建一个简单的 HTML CSS 作品集网站 我正在努力寻找一种方法来按照图片中显示的方式设置以下文本的样式 值得注意的是 我已经取得了一些成果 但问题是这里的文本不是从左到右居中的 这更多的是随机排列 我不知道如何实现这一点 另请记
  • 注释不同 Symfony 2 / Doctrine 2

    这两者有什么区别 ORM ManyToOne targetEntity Category inversedBy products cascade remove ORM JoinColumn name category id referenc
  • Vim 和 Ruby - 匹配“do”和“end”?

    我相信我们都熟悉 意外的 kEnd 问题 对我来说 追踪它们总是很麻烦 因为 Vim 不匹配 do 和 end 显然 其他以 end 结尾的元素 例如 if 有没有办法配置 Vim 显示这些匹配项以帮助简化调试 如果您使用的是最新的 vim
  • 为什么 Razor Pages 脚手架对索引、详细信息和删除页面使用 HTML 帮助程序(而不是标记帮助程序)?

    我最近使用 Razor Pages 开始了我的第一个项目 我使用 EF Core 并将我的所有模型搭建到 CRUD Razor Pages 中 我注意到生成的创建和编辑 Razor 页面使用标签帮助程序来显示数据 IE div class
  • 使用 jQuery.load 加载 html 后,如何使用 MathJax 格式化 html?

    我正在使用 jQuery load 加载新页面 然而 内容却以某种方式被奇怪地对待 在原始页面上 我有一些使用 MathJax 格式化乳胶命令的代码 这对于原始文件来说效果很好 但是 当我单击链接并将更多 HTML 插
  • 如何在 iOS 12 或更早版本中集成 Apple 登录? [复制]

    这个问题在这里已经有答案了 我的应用程序因第三方登录服务而被拒绝 因此 我为 iOS 13 实现了 使用 Apple 登录 问题是如何为 iOS 12 或更早版本提供向后兼容性 就我而言 只有当我避免导入时它才有效 适用于 iOS12 及更
  • 发布Python标准库的一部分

    如何将 Python 中的一些标准模块与我的代码一起发送 我正在为 Anki 编写一个附加组件 我需要它Queue and threading模块来自Python2 7标准库 当我尝试启动 Anki 时 我得到ImportError No
  • windows azure 中的 Svgs 和其他 mime 类型

    您好 我正在 Windows Azure 上的 NodeJS 中设置一个网站 并且希望包含 svg 作为背景图像 一般情况下我如何允许 svgs 和 mimetypes Azure 的用途IISNode https github com t
  • 制作 dplyr 过程的自定义函数

    我想为这个修改后的 dplyr 过程创建一个自定义函数 在 R 数据框中用 NA 随机替换每组值的百分比 https stackoverflow com questions 64034962 randomly replacing perce
  • 我的脚本运行时是否调用了 setInterval 处理程序?

    我计划使用 setInterval 简单地将一个变量设置为 false 主循环将检查该变量以停止 示例 注意 这只是一个示例 实际代码不是易于重构的 while 循环 而是实际上由闭源软件生成的相当复杂且执行时间较长的脚本 var runn
  • 如何使用应用程序名称杀死应用程序?

    有没有办法从应用程序名称获取包名称 例如 如果应用程序名称是 location 我可以获取其包名称 com android location 以便我可以使用 am killBackgroundProcesses 包名 或者有人知道任何可以使
  • 有没有办法激活控件 WebView 桌面模式而不是移动模式?

    有没有办法激活控件 WebView 桌面模式而不是移动模式
  • 在 SQL 中使用不同值更新多行

    我有一个这样的表 SKU Size A 10 B 10 C 10 D 10 E 10 F 10 G 10 我想把它改成 SKU Size A 20 B 10 C 30 D 10 E 80 F 10 G 60 我有超过 3000 行记录需要更
  • Work Light 6.1 Android 应用程序渲染所有视图,无需小部件

    我使用 Worklight 6 1 Dojo 1 8 开发了一个混合应用程序 该应用程序使用 Chrome Common Android 测试结果良好 当我将设备部署到运行 4 3 版本的 Nexus 7 平板电脑时 我的所有视图都会一起出
  • 如何在 Swift 中从二维码保存 vCard

    swift 中二维码的输出是一个字符串 我需要保存QR Code如果代码包含vCard with swift 我收到一条错误消息 指出它无法将 CNContacts 转换为 CNMutableContacts func foundCode
  • 如何在php中在特定日期或时间自动发送电子邮件?

    我开发了一个用于用户注册的网站 但我需要一个我从未体验过的功能 我需要的功能是向刚刚注册的用户发送一封电子邮件 并附加一个 pdf 文件 注册 15 天后 系统将自动向该用户发送一封电子邮件 请指导我如何使用 mySQL 数据库在 PHP
  • KRL RSS 解析器:处理编码问题?

    我正在将 RSS 提要从 Tumblr 导入到 Kynetx 应用程序中 RSS 提要似乎存在一些编码问题 因为撇号如下所示 提要 您可以找到here http instancevariable tumblr com rss 声称以 UTF
  • 更改 SwiftUI 中的弹出窗口大小

    我正在尝试为弹出窗口设置一定的大小或使其适应其内容 我尝试从弹出窗口更改视图的框架 但它似乎不起作用 Button Popover self popover7 toggle popover isPresented self popover7
  • 如何在 tkinter.ttk.Button 中创建箭头并控制其大小?

    我想创建带有箭头的 ttk button 并更改箭头大小 我发现 TButton 本质上包含 StyleNamesTButton leftarrow它不是由 ttk Style layout 公开的 问题 1 如何激活这些 StyleNam