Python Tkinter 自定义主题 [英] Python Tkinter custom themes

查看:48
本文介绍了Python Tkinter 自定义主题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

说明

我想使用 Tkinter/ttk 和 python 制作 GUI,原因是我想学习设计 GUI 的样式.

I want to make a GUI using Tkinter / ttk with python, the reason is because I want to learn to style GUI's.

我试图找到有关如何设置输入框"样式的信息,而不是背景,而是实际的插入框",但我找不到有关如何执行此操作的任何信息,并且内置主题隐藏得很好,因为我也找不到那些..

I have tried to find information about how to style the "Entry box" not the background but the actual "insert box" but I cant find any information about how to do this and the built in themes is quite well hidden because I cant find those either..

图片演示:

  • 默认样式

  • 我想要它

我的问题

  • 这是否可能?如果可能,如何实现?
  • 有没有办法访问默认主题以便向它们学习?

推荐答案

应用于 ttk.Entry 的标准样式根本不采用 fieldbackground 选项,这将改变文本输入字段的颜色.解决方案是创建一个响应选项的新元素.

The standard style applied to ttk.Entry simply doesn't take a fieldbackground option, which would be what changes the colour of the text entry field. The solution is this to create a new element that does respond to the option.

from tkinter import *
from tkinter import ttk

root_window = Tk()

estyle = ttk.Style()
estyle.element_create("plain.field", "from", "clam")
estyle.layout("EntryStyle.TEntry",
                   [('Entry.plain.field', {'children': [(
                       'Entry.background', {'children': [(
                           'Entry.padding', {'children': [(
                               'Entry.textarea', {'sticky': 'nswe'})],
                      'sticky': 'nswe'})], 'sticky': 'nswe'})],
                      'border':'2', 'sticky': 'nswe'})])

estyle.configure("EntryStyle.TEntry",
    fieldbackground="light blue")           # Set color here

entry = ttk.Entry(root_window, style="EntryStyle.TEntry")
entry.pack(padx=10, pady=10)

root_window.mainloop()

这篇关于Python Tkinter 自定义主题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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