- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我想知道您是否可以帮我解决 ttk 中的样式选项问题。我已经设法将大部分基本的 ttk 小部件更改为我喜欢的样式。我只是停留在改变滚动条的样式上。我已经搜索了几个小时寻找答案,不幸的是无济于事。
这是使用滚动条样式选项的示例代码:
import tkinter as tk
from tkinter import ttk
class Gui:
def __init__(self,mainframe):
#set the style
style = ttk.Style()
style.configure('Horizontal.TScrollbar',background = "blue" )
#Create a mainframe
self.mainframe = mainframe
self.mainframe.title("example")
#creating scrollbar frame
scrl_attr_frame = ttk.Frame(self.mainframe)
scrl_attr_frame.grid(column=0,row=5,sticky="ns")
scrl_attr_frame.rowconfigure(0, weight=1)
attr_canvas = tk.Canvas(scrl_attr_frame)
h_scroll = ttk.Scrollbar(scrl_attr_frame,orient="horizontal", command=attr_canvas.xview)
attr_canvas.configure(xscrollcommand=h_scroll.set)
attr_canvas.grid(column=0,row=0,sticky="ns")
h_scroll.grid(column=0, row=1,sticky="we")
attr_frame = ttk.Frame(attr_canvas)
attr_frame.grid(column=0,row=0,sticky="ns")
attr_canvas.create_window((0,0),window=attr_frame, anchor='nw')
attr_frame.bind("<Configure>",lambda event, canvas=attr_canvas : canvas.configure(scrollregion=canvas.bbox("all"),width=200,height=200,takefocus=False,highlightthickness=0))#attribute_frame.winfo_height()/20,highlightthickness=0))
#setup treeview widget
tree_columns = ("c1", "c2", "c3")
self.tree = ttk.Treeview(attr_frame,columns=tree_columns, show="headings",takefocus=False)
self.tree.grid(column=0, row=0, sticky='nsew')
for head in tree_columns:
self.tree.heading(head,text=head,anchor="w")
root = tk.Tk()
myapp = Gui(root)
root.mainloop()
我还尝试了几种组合,包括;
style.configure('TScrollbar',background='blue')
#and
style.configure('CustomScroll.Horizontal.TScrollbar',background='blue')
#in combination with
h_scroll = ttk.Scrollbar(scrl_attr_frame,orient="horizontal", command=attr_canvas.xview)
h_scroll['style'] = "CustomScroll.Horizontal.TScrollbar"
非常感谢您的帮助!
最佳答案
看起来您只是想更改 Windows 主题下水平滚动条的槽。 ttk 小部件由样式引擎提供的一组元素构成,并使用声明的布局进行组合。在 Windows 下,样式引擎是 Windows Visual Styles API,这意味着程序员无法控制用于绘制大多数常见元素的颜色或图像。按钮背景、滚动条槽和按钮以及拇指甚至滚动条拇指内部绘制的 handle 都是Windows提供的。
可以控制此应用程序自定义,但代价是使您的应用程序在给定平台上看起来不再是标准的。为此,您必须提供自己的 UI 元素并定义新的小部件布局。最终这可以变成定义您自己的主题。 ttk 库中的 tcl 脚本提供了很好的示例,甚至有一些完整的(如果是旧的)主题使用位图在 ttk 的原始版本中声明基于图像的主题元素,称为“tile”。
在这个特定的例子中,为了获得一个带有自定义彩色背景的 Windows 水平滚动条,我们需要重新定义布局以使用来自 Tk 绘制元素的滚动条槽。 “默认”主题中使用的元素可以被复制并使用样式配置参数定义,然后由 Tk 本身绘制,而不是传递给第三方引擎。以下代码生成这样的滚动条,它使用 vsapi 样式引擎提供的标准按钮和拇指,但替换了槽。这个导入的槽理解 troughcolor
样式配置选项,因此我们现在可以定义要使用的颜色。所有使用此样式的滚动条都将使用相同的颜色,因为小部件本身不会接受 troughcolor 选项。即:除非您为每种新颜色定义新样式,否则不能让一个滚动条为蓝色而另一个为红色。
from tkinter import *
from tkinter.ttk import *
def main():
app = Tk()
style = Style()
# import the 'trough' element from the 'default' engine.
style.element_create("My.Horizontal.Scrollbar.trough", "from", "default")
# Redefine the horizontal scrollbar layout to use the custom trough.
# This one is appropriate for the 'vista' theme.
style.layout("My.Horizontal.TScrollbar",
[('My.Horizontal.Scrollbar.trough', {'children':
[('Horizontal.Scrollbar.leftarrow', {'side': 'left', 'sticky': ''}),
('Horizontal.Scrollbar.rightarrow', {'side': 'right', 'sticky': ''}),
('Horizontal.Scrollbar.thumb', {'unit': '1', 'children':
[('Horizontal.Scrollbar.grip', {'sticky': ''})],
'sticky': 'nswe'})],
'sticky': 'we'})])
# Copy original style configuration and add our new custom configuration option.
style.configure("My.Horizontal.TScrollbar", *style.configure("Horizontal.TScrollbar"))
style.configure("My.Horizontal.TScrollbar", troughcolor="red")
# Create and show a widget using the custom style
hs = Scrollbar(app, orient="horizontal", style="My.Horizontal.TScrollbar")
hs.place(x=5, y=5, width=150)
hs.set(0.2,0.3)
app.mainloop()
if __name__ == '__main__':
main()
关于python - 在 tkinter 中更改滚动条的外观(使用 ttk 样式),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28375591/
在 How to make a Tkinter window jump to the front? 中提出的问题之后的一个问题 我希望有一个顶层窗口(我用它来导航我的其他主窗口)总是在前面。但我希望它
有没有办法在 Tkinter 中保持小部件(特别是图像)的堆叠顺序一致?例如,我可能在 Canvas 上的同一位置有两个矩形、两个三角形和一个圆。圆圈移动到最后一次点击鼠标的地方,但我总是希望它被绘制
这是一个简单的 GUI 程序,用于创建 5x16 按钮矩阵。 from tkinter import * root = Tk() button = [[0 for x in range(16)] fo
有一个错误:“AttributeError: module 'tkinter' has no attribute 'messagebox'” 即使 import tkinter 一开始就已经给出了,为
我知道 menu.tk_popup() 可用于在特定坐标处打开上下文菜单,但也不知道如何从中打开子菜单,如果这有意义的话。这是我编写的代码: import tkinter as tk root = t
我正在尝试在禁用自动换行和水平滚动条的文本窗口中书写,如下所示: root = Toplevel() root.geometry("%dx%d+0+0" % (350,400)) af=Frame(r
已经将文本变量分配给小部件后,如何将其删除? widget.config(textvariable=None)只是不工作。在谷歌或这里找不到任何东西。 最佳答案 将您的变量分配给一个空字符串以实现此目
Jython 支持 Tkinter 吗?如果我用 Python 编写一个程序并放一个 使用 Tkinter 的 GUI 前端,做同样的事情有多难 Jython 中的程序?或者对于 Jython GUI
因此,我尝试创建一个 tkinter 窗口,显示当前时间和日期以及自定义短语。不过,我遇到的问题是,我似乎无法在第二天刷新日期。 我可以传递截至运行代码时的当前日期,但之后它变为静态。 这是我目前的程
我的理解是在初始化 __init__ 中的所有框架和小部件之后方法,tkinter 窗口会调整大小以适合所有这些组件。 我想将窗口的初始化大小设置为其最小大小。我希望能够最大化并放大窗口,但我从不希望
此代码仅水平居中,如何使进度条也垂直居中? import Tkinter import ttk root = Tkinter.Tk() root.geometry("=500x500") root.p
使用 Python 2.7 和 Tkinter 模块,我创建了一个菜单按钮并为其分配了一个菜单。现在每次我在特定位置发布菜单时,菜单的宽度都会根据字符数自动设置。有没有办法在菜单小部件中设置静态宽度?
我想将我的 tkinter 应用程序的主题更改为 clam。 代码是什么,我把它放在哪里?我试过了: from tkinter import * from tkinter.ttk import * s
我有以下代码: from Tkinter import * from urllib import urlretrieve import webbrowser import ttk def get_la
我知道,如果我将滚动条控制的框架绑定(bind)到函数 ( onFrameConfigure ),您可以获得滚动条位置,如下所示:self.calendar_frame.bind("", self.o
许多网站都说菜单小部件有一个选项“字体”,但我一直无法设置它。系统是在 Windows 8.1 中运行的 Python 3.5。脚本开始: 从 tkinter 导入 * 根 = Tk() root.g
我正在阅读本教程,它帮助我同时学习 tkinter 和 wxWidgets,但我想深入挖掘,所以想知道哪个 GUI 工具更适合深入学习,为什么? 最佳答案 不可能说哪个“更好”。两者均可用于最常见的用
看书学python,tkinter.END用在一段代码里不用解释 import tkinter def count(text, out_data): """ Update out_data w
我正在尝试使用 Python 2.7 将 Tkinter 导入到我的项目中,但我收到了错误: ImportError: No module named tkinter 在有人说之前,我已经尝试了“Tk
当我回答 Tkinter 问题时,我通常会尝试自己运行代码,但有时我会收到此错误: Traceback (most recent call last): File "C:\Python27\pyg
我是一名优秀的程序员,十分优秀!