问题描述
我们正在使用 Python+tkinter 构建一个 GUI 界面.问题是当我们想要设置实体的视图模式时.我需要将树视图小部件的视图模式或状态设置为已禁用".
We're building a GUI interface with Python+tkinter.The problem is when we want to set the view mode of an entity. I need to set the view mode or state of the treeview widget as 'disabled'.
我们该如何解决?感谢您的支持.
How can we solve it?Thanks for any support.
更新
self.frmTab01.trvDetailorder.configure(selectmode='none')
我正在寻找一种解决方案,在该解决方案中,应用程序禁用选择,影响小部件的可视化,就像入口小部件一样.
I'm looking for a solution in which appart from disable the selection, affect the visualization of the widget just like an entry widget.
推荐答案
nbro 是对的,您需要更改 Treeview 样式以使其看起来禁用.此外,我还使用鼠标单击时的绑定技巧禁用了在禁用 Treeview 时打开/关闭项目的可能性.
nbro is right, you need to change the Treeview style to make it look disabled. In addition, I also deactivated the possibility to open/close items when the Treeview is disabled using binding tricks on the mouse click.
在我的示例中,我添加了一个条目,以便您可以比较两个小部件的外观.如果您使用的是 OS X 或 Windows,您可能需要更改主题(style.theme_use("clam")
应该这样做),因为它们的默认主题不是很可定制.
In my example I added an entry so that you can compare the look on the two widgets. If you are using OS X or Windows, you might need to change the theme (style.theme_use("clam")
should do) because their default themes are not very customizable.
from tkinter import Tk
from tkinter.ttk import Treeview, Style, Button, Entry
root = Tk()
def toggle_state():
if "disabled" in tree.state():
e.state(("!disabled",))
tree.state(("!disabled",))
# re-enable item opening on click
tree.unbind('<Button-1>')
else:
e.state(("disabled",))
tree.state(("disabled",))
# disable item opening on click
tree.bind('<Button-1>', lambda e: 'break')
style = Style(root)
# get disabled entry colors
disabled_bg = style.lookup("TEntry", "fieldbackground", ("disabled",))
disabled_fg = style.lookup("TEntry", "foreground", ("disabled",))
style.map("Treeview",
fieldbackground=[("disabled", disabled_bg)],
foreground=[("disabled", "gray")],
background=[("disabled", disabled_bg)])
e = Entry()
e.insert(0, "text")
e.pack()
tree = Treeview(root, selectmode='none')
tree.pack()
tree.insert("", 0, iid="1", text='1')
tree.insert("1", 0, iid='11', text='11')
Button(root, text="toggle", command=toggle_state).pack()
root.mainloop()
这篇关于tkinter treeview:如何禁用小部件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!