How to create a tree view with checkboxes in Python - python

I've been using Tkinter and Tix to write a small program.
I'm at a point where I need a tree view with checkboxes (checkbuttons) so I can select items from the tree view.
Is there an easy way to do this?
I've been looking at ttk.Treeview () and it looks easy to get the tree view but is there a way to insert a checkbutton to the view?
A simple code snippet would be really appreciated.
I'm not limited to ttk. Anything will do; as long as I have an example or good docs I can make it work

import Tix
class View(object):
def __init__(self, root):
self.root = root
self.makeCheckList()
def makeCheckList(self):
self.cl = Tix.CheckList(self.root, browsecmd=self.selectItem)
self.cl.pack()
self.cl.hlist.add("CL1", text="checklist1")
self.cl.hlist.add("CL1.Item1", text="subitem1")
self.cl.hlist.add("CL2", text="checklist2")
self.cl.hlist.add("CL2.Item1", text="subitem1")
self.cl.setstatus("CL2", "on")
self.cl.setstatus("CL2.Item1", "on")
self.cl.setstatus("CL1", "off")
self.cl.setstatus("CL1.Item1", "off")
self.cl.autosetmode()
def selectItem(self, item):
print item, self.cl.getstatus(item)
def main():
root = Tix.Tk()
view = View(root)
root.update()
root.mainloop()
if __name__ == '__main__':
main()

I made a treeview class with checkboxes inheriting ttk.Treeview, but the checkboxes are not ttk.Checkbutton but images of checked, unchecked and tristate checkboxes.
import tkinter as tk
import tkinter.ttk as ttk
class CheckboxTreeview(ttk.Treeview):
"""
Treeview widget with checkboxes left of each item.
The checkboxes are done via the image attribute of the item, so to keep
the checkbox, you cannot add an image to the item.
"""
def __init__(self, master=None, **kw):
ttk.Treeview.__init__(self, master, **kw)
# checkboxes are implemented with pictures
self.im_checked = tk.PhotoImage(file='checked.png')
self.im_unchecked = tk.PhotoImage(file='unchecked.png')
self.im_tristate = tk.PhotoImage(file='tristate.png')
self.tag_configure("unchecked", image=self.im_unchecked)
self.tag_configure("tristate", image=self.im_tristate)
self.tag_configure("checked", image=self.im_checked)
# check / uncheck boxes on click
self.bind("<Button-1>", self.box_click, True)
def insert(self, parent, index, iid=None, **kw):
""" same method as for standard treeview but add the tag 'unchecked'
automatically if no tag among ('checked', 'unchecked', 'tristate')
is given """
if not "tags" in kw:
kw["tags"] = ("unchecked",)
elif not ("unchecked" in kw["tags"] or "checked" in kw["tags"]
or "tristate" in kw["tags"]):
kw["tags"] = ("unchecked",)
ttk.Treeview.insert(self, parent, index, iid, **kw)
def check_descendant(self, item):
""" check the boxes of item's descendants """
children = self.get_children(item)
for iid in children:
self.item(iid, tags=("checked",))
self.check_descendant(iid)
def check_ancestor(self, item):
""" check the box of item and change the state of the boxes of item's
ancestors accordingly """
self.item(item, tags=("checked",))
parent = self.parent(item)
if parent:
children = self.get_children(parent)
b = ["checked" in self.item(c, "tags") for c in children]
if False in b:
# at least one box is not checked and item's box is checked
self.tristate_parent(parent)
else:
# all boxes of the children are checked
self.check_ancestor(parent)
def tristate_parent(self, item):
""" put the box of item in tristate and change the state of the boxes of
item's ancestors accordingly """
self.item(item, tags=("tristate",))
parent = self.parent(item)
if parent:
self.tristate_parent(parent)
def uncheck_descendant(self, item):
""" uncheck the boxes of item's descendant """
children = self.get_children(item)
for iid in children:
self.item(iid, tags=("unchecked",))
self.uncheck_descendant(iid)
def uncheck_ancestor(self, item):
""" uncheck the box of item and change the state of the boxes of item's
ancestors accordingly """
self.item(item, tags=("unchecked",))
parent = self.parent(item)
if parent:
children = self.get_children(parent)
b = ["unchecked" in self.item(c, "tags") for c in children]
if False in b:
# at least one box is checked and item's box is unchecked
self.tristate_parent(parent)
else:
# no box is checked
self.uncheck_ancestor(parent)
def box_click(self, event):
""" check or uncheck box when clicked """
x, y, widget = event.x, event.y, event.widget
elem = widget.identify("element", x, y)
if "image" in elem:
# a box was clicked
item = self.identify_row(y)
tags = self.item(item, "tags")
if ("unchecked" in tags) or ("tristate" in tags):
self.check_ancestor(item)
self.check_descendant(item)
else:
self.uncheck_descendant(item)
self.uncheck_ancestor(item)
if __name__ == '__main__':
root = tk.Tk()
t = CheckboxTreeview(root, show="tree")
t.pack()
t.insert("", 0, "1", text="1")
t.insert("1", "end", "11", text="1")
t.insert("1", "end", "12", text="2")
t.insert("12", "end", "121", text="1")
t.insert("12", "end", "122", text="2")
t.insert("122", "end", "1221", text="1")
t.insert("1", "end", "13", text="3")
t.insert("13", "end", "131", text="1")
root.mainloop()
An improved version of CheckboxTreeview is available in the ttkwidgets module.

If you can use Tix, go with #Brandon's solution. If you are stuck with Ttk (as I am), here is an solution based on #j_4231's idea. Rather than using an image to represent the checkbox, we can use two characters provided by Unicode:
'BALLOT BOX' (U+2610) : ☐
'BALLOT BOX WITH X (U+2612)' : ☒.
Those character are located after the item name and are used to check the current state: treeview.item(iid, "text")[-1] is either ☐ or ☒. We can update the item name when the text is clicked.
The class TtkCheckList inherits ttk.Treeview, hence the usual parameters/methods of Treeview can be used.
import tkinter as tk
from tkinter import ttk
BALLOT_BOX = "\u2610"
BALLOT_BOX_WITH_X = "\u2612"
class TtkCheckList(ttk.Treeview):
def __init__(self, master=None, width=200, clicked=None, separator='.',
unchecked=BALLOT_BOX, checked=BALLOT_BOX_WITH_X, **kwargs):
"""
:param width: the width of the check list
:param clicked: the optional function if a checkbox is clicked. Takes a
`iid` parameter.
:param separator: the item separator (default is `'.'`)
:param unchecked: the character for an unchecked box (default is
"\u2610")
:param unchecked: the character for a checked box (default is "\u2612")
Other parameters are passed to the `TreeView`.
"""
if "selectmode" not in kwargs:
kwargs["selectmode"] = "none"
if "show" not in kwargs:
kwargs["show"] = "tree"
ttk.Treeview.__init__(self, master, **kwargs)
self._separator = separator
self._unchecked = unchecked
self._checked = checked
self._clicked = self.toggle if clicked is None else clicked
self.column('#0', width=width, stretch=tk.YES)
self.bind("<Button-1>", self._item_click, True)
def _item_click(self, event):
assert event.widget == self
x, y = event.x, event.y
element = self.identify("element", x, y)
if element == "text":
iid = self.identify_row(y)
self._clicked(iid)
def add_item(self, item):
"""
Add an item to the checklist. The item is the list of nodes separated
by dots: `Item.SubItem.SubSubItem`. **This item is used as `iid` at
the underlying `Treeview` level.**
"""
try:
parent_iid, text = item.rsplit(self._separator, maxsplit=1)
except ValueError:
parent_iid, text = "", item
self.insert(parent_iid, index='end', iid=item,
text=text+" "+self._unchecked, open=True)
def toggle(self, iid):
"""
Toggle the checkbox `iid`
"""
text = self.item(iid, "text")
checked = text[-1] == self._checked
status = self._unchecked if checked else self._checked
self.item(iid, text=text[:-1] + status)
def checked(self, iid):
"""
Return True if checkbox `iid` is checked
"""
text = self.item(iid, "text")
return text[-1] == self._checked
def check(self, iid):
"""
Check the checkbox `iid`
"""
text = self.item(iid, "text")
if text[-1] == self._unchecked:
self.item(iid, text=text[:-1] + self._checked)
def uncheck(self, iid):
"""
Uncheck the checkbox `iid`
"""
text = self.item(iid, "text")
if text[-1] == self._checked:
self.item(iid, text=text[:-1] + self._unchecked)
Here is an example:
items = [
'Item',
'Item.SubItem1',
'Item.SubItem2',
'Item.SubItem2.SubSubItem1',
'Item.SubItem2.SubSubItem2',
'Item.SubItem2.SubSubItem3',
'Item.SubItem3',
'Item.SubItem3.SubSubItem1',
'Item.SubItem4'
]
root = tk.Tk()
root.title('Test')
root.geometry('400x300')
check_list = TtkCheckList(root, height=len(items))
for item in items:
check_list.add_item(item)
check_list.pack()
root.mainloop()
You can use the clicked parameter to define a new behavior when an item is
clicked. For instance:
def obey_ancestor(iid):
"""
If the status of an item is toggled, the status of all its descendants
is also set to the new status.
"""
set_status = check_list.uncheck if check_list.checked(iid) else check_list.check
stack = [iid]
while stack:
iid = stack.pop()
set_status(iid)
stack.extend(check_list.get_children(iid))
And:
check_list = TtkCheckList(root, height=len(items),
clicked=obey_ancestor)

I would add to jferard's great answer that if you want to have a table of values rather than a tree structure change the following:
In the init add:
self.column('#1', width=width, stretch=tk.YES)
for each column you want.
add_item should be:
def add_item(self, item):
"""
Add an item to the checklist. The item is the list of nodes separated
by dots: `Item.SubItem.SubSubItem`. **This item is used as `iid` at
the underlying `Treeview` level.**
"""
# try:
# parent_iid, text = item.rsplit(self._separator, maxsplit=1)
# except ValueError:
# parent_iid, text = "", item
# self.insert(parent_iid, index='end', iid=item, text=text+" "+self._unchecked, open=True)
self.insert('', index='end', iid=item, values = item, text=self._unchecked, open=True)
Change the example as such:
cols = ['One', 'Two']
items = [('A', '1',),('B','2')]
check_list = TtkCheckList(root, columns = cols, height=len(items))

Related

Python GTK 3 - TreeView - Allow mouse selection on the row for copying contents

I'm new on Python GTK and I was developing a simple TreeView that show several data:
import gi
gi.require_version("Gtk", "3.0")
from gi.repository import Gtk
# list of tuples for each variable, containing the environment variable name, its path, and the security category
variable_list = [
("$ENV1", "/usr/share/background/image.png", "Image"),
("$ENV2", "/home/user/script.py", "Program"),
("$ENV3", "/usr/share/icons/icon.png", "Image")
]
class TreeViewFilterWindow(Gtk.Window):
def __init__(self):
super().__init__(title="Environment Variable View")
self.set_border_width(10)
# Setting up the self.grid in which the elements are to be positioned
self.grid = Gtk.Grid()
self.grid.set_column_homogeneous(True)
self.grid.set_row_homogeneous(True)
self.add(self.grid)
# Creating the ListStore model
self.variable_liststore = Gtk.ListStore(str, str, str)
for variable_ref in variable_list:
self.variable_liststore.append(list(variable_ref))
self.current_filter_category = None
# Creating the filter, feeding it with the liststore model
self.category_filter = self.variable_liststore.filter_new()
# setting the filter function, note that we're not using the
self.category_filter.set_visible_func(self.category_filter_func)
# creating the treeview, making it use the filter as a model, and adding the columns
self.treeview = Gtk.TreeView(model=self.category_filter)
for i, column_title in enumerate(
["Variable", "Path", "Category"]
):
renderer = Gtk.CellRendererText()
column = Gtk.TreeViewColumn(column_title, renderer, text=i)
self.treeview.append_column(column)
# creating buttons to filter by Category, and setting up their events
self.buttons = list()
for var_category in ["Image", "Program"]:
button = Gtk.Button(label=var_category)
self.buttons.append(button)
button.connect("clicked", self.on_selection_button_clicked)
# setting up the layout, putting the treeview in a scrollwindow, and the buttons in a row
self.scrollable_treelist = Gtk.ScrolledWindow()
self.scrollable_treelist.set_vexpand(True)
self.grid.attach(self.scrollable_treelist, 0, 0, 5, 10)
self.grid.attach_next_to(
self.buttons[0], self.scrollable_treelist, Gtk.PositionType.BOTTOM, 1, 1
)
for i, button in enumerate(self.buttons[1:]):
self.grid.attach_next_to(
button, self.buttons[i], Gtk.PositionType.RIGHT, 1, 1
)
self.scrollable_treelist.add(self.treeview)
self.show_all()
def category_filter_func(self, model, iter, data):
"""Tests if the Category in the row is the one in the filter"""
if (
self.current_filter_category is None
or self.current_filter_category == "None"
):
return True
else:
return model[iter][2] == self.current_filter_category
def on_selection_button_clicked(self, widget):
"""Called on any of the button clicks"""
# we set the current category filter to the button's label
self.current_filter_category = widget.get_label()
print("%s category selected!" % self.current_filter_category)
# we update the filter, which updates in turn the view
self.category_filter.refilter()
win = TreeViewFilterWindow()
win.connect("destroy", Gtk.main_quit)
win.show_all()
Gtk.main()
When I run the script, the content of each row cannot be selected. Is there a way to select the content of these rows in order that I can right-click and copy the content?
In case I would like to highlight with the mouse cursor the text and then directly copy the highlighted content on the clipboard with a small message saying "The content has been copied", how could I reach these purposes?
If selection was possible, this would have to be done by setting a property in Gtk.CellRendererText, but I don't see a property that would make that possible, besides editable:
renderer.set_property("editable", True)
Making the cells editable would certainly allow users to copy from them, but this would suggest the contents could be changed. (If you don't connect to the edited signal, the text reverts after the input. This might feel very counterintuitive.)
If you the time, you could maybe write your own CellRenderer (I have no experience with this)
Another possibility would be to connect to the button-released-event of the Gtk.TreeView and copy the text to the clipboard if the user right-clicks.
Use Gtk.TreeView.get_path_at_pos to get the cell under the cursor.
def listclick(self, listview, event: Gdk.EventButton, *_unused) -> bool:
"""Handler for clicks on the list."""
if (event.type == Gdk.EventType.BUTTON_RELEASE and event.button == 3):
tup = listview.get_path_at_pos(event.x, event.y)
if tup is not None:
path, col, *unused = tup
# Copy to Clipboard, depending on Path and Column
# ...
return True
return False

PyQt5: How to reset the content of a QtWidgets.QGroupBox

I have a RadioButtonWidget class that receives a list of names (button_list) and a QtWidgets.QGroupBox (radio_group_box) and creates a radio button for each name. The problem I have is that after creating the buttons, I cannot change them. That is if I call the class again with another list of names, nothing changes. I need to create a function inside my class to remove any existing radio buttons so that I can add a new list inside it.
I tried to do radio_group_box.deleteLater() outside the class but this removes the whole box.
class RadioButtonWidget(QtWidgets.QWidget):
def __init__(self, radio_group_box, button_list):
super().__init__()
self.radio_group_box = radio_group_box
self.radio_button_group = QtWidgets.QButtonGroup()
#create the radio buttons
self.radio_button_list = []
for each in button_list:
self.radio_button_list.append(QtWidgets.QRadioButton(each))
if button_list != []:
#set the default checked item
self.radio_button_list[0].setChecked(True)
#create layout for radio buttons and add them
self.radio_button_layout = QtWidgets.QVBoxLayout()
# add buttons to the layout and button group
counter = 1
for each in self.radio_button_list:
self.radio_button_layout.addWidget(each)
self.radio_button_group.addButton(each)
self.radio_button_group.setId(each,counter)
counter += 1
# add radio buttons to the group box
self.radio_group_box.setLayout(self.radio_button_layout)
def selected_button(self):
return self.radio_button_group.checkedId()
Instead of removing the radio buttons, you can create a whole new radio button layout and set it for the group box exactly as you did in the constructor. Here is an example where the function set_group_box_buttons will remove the existing layout from radio_group_box (which is done by setting it to a temp widget), and add a new one with the new buttons.
import sys
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
from PyQt5.QtGui import *
class RadioButtonWidget(QWidget):
def __init__(self, radio_group_box, button_list):
super().__init__()
self.radio_group_box = radio_group_box
self.set_group_box_buttons(button_list)
grid = QGridLayout(self)
grid.addWidget(self.radio_group_box)
def selected_button(self):
return self.radio_button_group.checkedId()
def set_group_box_buttons(self, button_list):
self.radio_button_group = QButtonGroup()
self.radio_button_list = [QRadioButton(x) for x in button_list]
if button_list:
self.radio_button_list[0].setChecked(True)
if self.radio_group_box.layout():
QWidget().setLayout(self.radio_group_box.layout())
self.radio_button_layout = QVBoxLayout()
for i, v in enumerate(self.radio_button_list):
self.radio_button_layout.addWidget(v)
self.radio_button_group.addButton(v)
self.radio_button_group.setId(v, i)
self.radio_group_box.setLayout(self.radio_button_layout)
class Template(QWidget):
def __init__(self):
super().__init__()
self.rbw = RadioButtonWidget(QGroupBox('Radio Buttons'), ['Radio 1', 'Radio 2', 'Radio 3'])
self.box = QLineEdit()
self.box.returnPressed.connect(self.replace_buttons)
grid = QGridLayout(self)
grid.addWidget(self.rbw, 0, 0)
grid.addWidget(self.box, 0, 1)
def replace_buttons(self):
self.rbw.set_group_box_buttons(self.box.text().split(', '))
if __name__ == '__main__':
app = QApplication(sys.argv)
gui = Template()
gui.show()
sys.exit(app.exec_())
To demonstrate, I added a QLineEdit which will update the names when you press enter. Before:
After:
There's a conceptual error in your code: you are creating a new RadioButtonGroup, which is a widget, but you are not using it.
As long as each group box will only contain the radio buttons, there is no need to create a new widget (especially if you're not actually using it); you just have to create a layout if the groupbox doesn't have one yet.
There are at least two possible approaches to your question.
For both of them I always use existing radios if possible, to avoid unnecessary object destruction each time the options change, so that they are removed only when the number of options decreases. This also avoids unnecessary layout updates (especially if the number of options is the same).
I also kept the logical "interface" consistent, providing the same method and behavior of update_options(groupBox, options).
QObject based group
With this implementation, I'm creating an object that acts as an interface responsible of creating a QButtonGroup and setting the options, while also providing signals for the state change or the current checked radio.
class RadioButtonGroup(QtCore.QObject):
optionToggled = QtCore.pyqtSignal(object, int, bool)
optionChanged = QtCore.pyqtSignal(object, int)
def __init__(self, radio_group_box, button_list):
super().__init__()
self.groupBox = radio_group_box
layout = radio_group_box.layout()
self.buttonGroup = QtWidgets.QButtonGroup(self)
self.buttonGroup.buttonToggled[int, bool].connect(self.changed)
if layout is None:
layout = QtWidgets.QVBoxLayout(radio_group_box)
for i, text in enumerate(button_list, 1):
radio = QtWidgets.QRadioButton(text)
layout.addWidget(radio)
self.buttonGroup.addButton(radio, i)
def button(self, id):
return self.buttonGroup.button(id)
def changed(self, i, state):
self.optionToggled.emit(self, i, state)
if state:
self.optionChanged.emit(self, i)
def selected_button(self):
return self.buttonGroup.checkedId()
def update_options(self, button_list):
layout = self.groupBox.layout()
# this method will keep the current checked radio as checked, if you want
# to reset it everytime, just uncomment the next commented lines
#self.buttonGroup.setExclusive(False)
for i, text in enumerate(button_list, 1):
radio = self.buttonGroup.button(i)
if radio:
#radio.setChecked(False)
radio.setText(text)
else:
radio = QtWidgets.QRadioButton(text)
layout.addWidget(radio)
self.buttonGroup.addButton(radio, i)
#self.buttonGroup.setExclusive(True)
if len(button_list) == len(self.buttonGroup.buttons()):
return
# there are more radios than needed, remove them
for radio in self.buttonGroup.buttons():
id = self.buttonGroup.id(radio)
if id > i:
self.buttonGroup.removeButton(radio)
radio.deleteLater()
class ObjectBased(QtWidgets.QMainWindow):
def __init__(self):
super().__init__()
uic.loadUi('buttongroup.ui', self)
self.pushButton.clicked.connect(self.setOptions)
self.groupBoxes = self.groupBox1, self.groupBox2, self.groupBox3
self.radioButtonGroups = []
for box in self.groupBoxes:
group = RadioButtonGroup(box,
['Option {}'.format(o + 1) for o in range(randrange(1, 10))])
self.radioButtonGroups.append(group)
group.optionChanged.connect(self.optionChanged)
def setOptions(self):
buttonGroup = self.radioButtonGroups[self.comboBox.currentIndex()]
options = ['Option {}'.format(o + 1) for o in range(self.spinBox.value())]
buttonGroup.update_options(options)
def optionChanged(self, radioButtonGroup, id):
groupBox = radioButtonGroup.groupBox
print('{} checked {} ({})'.format(
groupBox.title(), id, radioButtonGroup.button(id).text()))
Self contained
In this mode, the logic is all within the window class. While this approach is slightly simpler than the other one, we're missing an unique "interface", which might be useful for access from external objects instead.
class SelfContained(QtWidgets.QMainWindow):
def __init__(self):
super().__init__()
uic.loadUi('buttongroup.ui', self)
self.pushButton.clicked.connect(self.setOptions)
self.radioButtonGroups = []
for g, groupBox in enumerate((self.groupBox1, self.groupBox2, self.groupBox3)):
buttonGroup = QtWidgets.QButtonGroup(self)
self.radioButtonGroups.append((groupBox, buttonGroup))
buttonGroup.buttonToggled[int, bool].connect(lambda id, state, g=g: self.optionChanged(g, id, state))
self.update_options(g, ['Option {}'.format(o + 1) for o in range(randrange(1, 10))])
def update_options(self, groupId, button_list):
groupBox, buttonGroup = self.radioButtonGroups[groupId]
layout = groupBox.layout()
if layout is None:
layout = QtWidgets.QVBoxLayout(groupBox)
# as above...
#buttonGroup.setExclusive(False)
for i, text in enumerate(button_list, 1):
radio = buttonGroup.button(i)
if radio:
#radio.setChecked(False)
radio.setText(text)
else:
radio = QtWidgets.QRadioButton(text)
layout.addWidget(radio)
buttonGroup.addButton(radio, i)
#buttonGroup.setExclusive(True)
if len(button_list) == len(buttonGroup.buttons()):
return
for radio in buttonGroup.buttons():
id = buttonGroup.id(radio)
if id > i:
buttonGroup.removeButton(radio)
radio.deleteLater()
def setOptions(self):
groupId = self.comboBox.currentIndex()
options = ['Option {}'.format(o + 1) for o in range(self.spinBox.value())]
self.update_options(groupId, options)
def optionChanged(self, groupId, id, state):
if state:
groupBox, buttonGroup = self.radioButtonGroups[groupId]
print('{} checked {} ({})'.format(groupBox.title(), id, buttonGroup.button(id).text()))

Tkhtml with python 3.5

I was searching for Tkinter widget for displaying webpage/Url/ads in my Tkinter window widget.and after some google/stack search, I found a widget called (tk_html_widget) and this page where I found Tkhtml...but I don't know how to use both(tk_html_widgets and Tkhtml). Please refer me something for study these(tk_html_widgets and Tkhtml)
Thank you
for an example of hyperlink type behaviour:
__all__ = ["LinkScrolledText"]
try:
from tkinter import scrolledtext
except ImportError:
import ScrolledText as scrolledtext
class HyperlinkManager(object):
"""A class to easily add clickable hyperlinks to Text areas.
Usage:
callback = lambda : webbrowser.open("http://www.google.com/")
text = tk.Text(...)
hyperman = tkHyperlinkManager.HyperlinkManager(text)
text.insert(tk.INSERT, "click me", hyperman.add(callback))
From http://effbot.org/zone/tkinter-text-hyperlink.htm
"""
def __init__(self, text, statusfunc=None):
self.text = text
self.statusfunc = statusfunc
self.text.tag_config("hyper", foreground="blue", underline=1)
self.text.tag_bind("hyper", "<Enter>", self._enter)
self.text.tag_bind("hyper", "<Leave>", self._leave)
self.text.tag_bind("hyper", "<Button-1>", self._click)
self.reset()
def reset(self):
self.links = {}
def add(self, action, tooltip=None):
"""Adds an action to the manager.
:param action: A func to call.
:return: A clickable tag to use in the text widget.
"""
tag = "hyper-%d" % len(self.links)
self.links[tag] = [action, tooltip]
return ("hyper", tag)
def _enter(self, event):
self.text.config(cursor="hand2")
for tag in self.text.tag_names(tk.CURRENT):
if (tag[:6] == "hyper-"):
tooltip = self.links[tag][1]
if self.statusfunc:
self.statusfunc(tooltip) # don't care if no tooltip as function clears if it doesn't
return
def _leave(self, event):
self.text.config(cursor="")
if self.statusfunc:
self.statusfunc()
def _click(self, event):
for tag in self.text.tag_names(tk.CURRENT):
if (tag[:6] == "hyper-"):
func = self.links[tag][0]
if func:
func()
return
class LinkScrolledText(scrolledtext.ScrolledText):
"""A class to add hyperlink functionality to a scrolledtext widget
the link does not actually have to be an actual hyperlink,
just a callable action.
an optional tooltip can be provided that will be displayed in the bottom left
just like a url would be in a browser when hovering over a link.
"""
def __init__(self, master=None, *args, **kwargs):
scrolledtext.ScrolledText.__init__(self, master, *args, **kwargs)
self.status = tk.Label(self)
self._hyper = HyperlinkManager(self, self._showstatus)
self.reset_links()
def _showstatus(self, status=None):
if status:
self.status.configure(text=status)
self.status.place(relx=0, rely=1, anchor='sw')
else:
self.status.place_forget()
def reset_links(self):
self._hyper.reset()
def insert_hyperlink(self, position, text, action, tag=None, tooltip=None):
tags = self._hyper.add(action, tooltip)
if type(tag) == list:
tags = tags + tag
elif tag != None:
tags.append(tag)
self.insert(position, text, tags)
if __name__ == "__main__":
try:
import tkinter as tk
except ImportError:
import Tkinter as tk
root = tk.Tk()
tb = LinkScrolledText(root)
tb.pack(fill="both", expand=True)
tb.insert_hyperlink("end", "Test", action=None, tooltip="This is a test")
root.mainloop()

Python Tkinter: Tree selection

I have created 2 trees with idlelib.TreeWidget in Canvas, left and right.
I am also able to print out the name of a tree node if double-clicked, but what I need is double-clicking one tree node will make a certain tree node visible and selected.
I have a simple example here. If you double click "level1" on the left hand side, "ccc" on the right hand side should be visible and automatically selected. How do you do that?
Please run the following code:
from Tkinter import Tk, Frame, BOTH, Canvas
from xml.dom.minidom import parseString
from idlelib.TreeWidget import TreeItem, TreeNode
class DomTreeItem(TreeItem):
def __init__(self, node):
self.node = node
def GetText(self):
node = self.node
if node.nodeType == node.ELEMENT_NODE:
return node.nodeName
elif node.nodeType == node.TEXT_NODE:
return node.nodeValue
def IsExpandable(self):
node = self.node
return node.hasChildNodes()
def GetSubList(self):
parent = self.node
children = parent.childNodes
prelist = [DomTreeItem(node) for node in children]
itemlist = [item for item in prelist if item.GetText().strip()]
return itemlist
def OnDoubleClick(self):
print self.node.nodeName
left = '''
<level0>
<level1/>
</level0>
'''
right = '''
<aaa>
<bbb> <ccc/> </bbb>
</aaa>
'''
class Application(Frame):
def __init__(self, parent):
Frame.__init__(self, parent)
self.parent = parent
self.parent.geometry('%dx%d+%d+%d' % (800, 300, 0, 0))
self.parent.resizable(0, 0)
dom = parseString(left)
item = DomTreeItem(dom.documentElement)
self.canvas = Canvas(self, bg = "cyan")
self.canvas.grid(column = 0, row = 0, sticky = 'NSWE')
node = TreeNode(self.canvas, None, item)
node.update()
dom2 = parseString(right)
item2 = DomTreeItem(dom2.documentElement)
self.canvas2 = Canvas(self, bg = "yellow")
self.canvas2.grid(column = 1, row = 0, sticky = 'NSWE')
node2 = TreeNode(self.canvas2, None, item2)
node2.update()
self.pack(fill = BOTH, expand = True)
def main():
root = Tk()
Application(root)
root.mainloop()
if __name__ == '__main__':
main()
First, your double click callback must be aware of your TreeNode node2 (I can think of global variable, attribute in DomTreeItem or bounce to another component).
Then you can rely on expand() method of TreeNode, read the children attribute and expand sequentially until the element you want. Note that children attribute is only populated after the node has been expanded.
1. Quick answer
Quick and dirty solution for the example you have provided
class DomTreeItem(TreeItem):
def OnDoubleClick(self):
if self.GetText() == "level1":
node2.expand()
node2.children[0].expand()
node2.children[0].children[0].select()
[...]
class Application(Frame):
def __init__(self, parent):
global node2
2. Generic solution
Here is a more general method to display an arbitrary item in a tree.
def reach(node_tree, path):
tokens = path.split("/")
current_node = node_tree
for name in tokens:
if len(current_node.children) == 0 and current_node.state != "expanded":
current_node.expand()
candidates = [child for child in current_node.children if child.item.GetText() == name]
if len(candidates) == 0:
print("did not find '{}'".format(name))
return
current_node = candidates[0]
current_node.select()
You might use it this way
if self.GetText() == "level1":
reach(node2, "bbb/ccc")
3. Architecture proposal
Besides the expansion an selection of an item, I propose you a cleaner architecture through a DIY observer.
DIY Observer
(mimic the Tkinter bind call but does not rely on tkinter machinery since generating event with user data is not properly handled)
class DomTreeItem(TreeItem):
def __init__(self, node, dbl_click_bindings = None):
self.node = node
self.dbl_click_bindings = [] if (dbl_click_bindings == None) else dbl_click_bindings
[...]
def OnDoubleClick(self):
self.fireDblClick()
def bind(self, event, callback):
'''mimic tkinter bind
'''
if (event != "<<TreeDoubleClick>>"):
print("err...")
self.dbl_click_bindings.append(callback)
def fireDblClick(self):
for callback in self.dbl_click_bindings:
callback.double_click(self.GetText())
Dedicated component
Rely on the reach method presented above.
class TreeExpander:
def __init__(self, node_tree, matching_items):
self.node_tree = node_tree
self.matching_items = matching_items
def double_click(self, item_name):
print("double_click ({0})".format(item_name))
if (item_name in self.matching_items):
reach(self.node_tree, self.matching_items[item_name])
Subscription
class Application(Frame):
def __init__(self, parent):
[...]
expander = TreeExpander(node2, {
"level1": "bbb/ccc"
})
item.bind("<<TreeDoubleClick>>", expander)
I did not find docs for idlelib, in this case, you can try to look at the code. The following snippet allows you to find which file host this module.
import idlelib.TreeWidget
print(idlelib.TreeWidget.__file__)

Activate other widgets while mouse dragging

I have multiple Tkinter.labels in a row and i would like the user to be able to click and drag their mouse over each one, activating them in the process.
I know about bindings, but i need multiple events in a single binding. Ive been messing around with <Button-1> and <Enter>, however i need a callback to be called only when both are true.
I know l.bind('<Button-1>,<Enter>', ...) is not valid.
Anyone with more Tkinter experience know of a way to chain binding, or make a multi-bind??
The way you solve this particular problem is to have a binding on ButtonPress and ButtonRelease that sets a flag. Then, in your binding for <Enter> (or any other event) you check for that flag.
However, while the button is pressed you won't get any <Enter> events. This is because the widget you clicked over grabs the pointer and owns it until you release the button. The only <Enter> events you'll get while the button is pressed are when you enter the widget you originally clicked on.
What you want to do instead is bind to <B1-Motion>. You can then use the x/y coordinates of the event and winfo_containing to determine what widget you are over.
That being said, trying to simulate selection over a row of labels is a lot of work for very little benefit. Why not just use a text widget that already has selection built in? You can tweak it so that it looks like a bunch of labels (ie: make the background the same color as a frame) and you can turn editing off. That might be an easier way to go.
I encountered this same problem today and thanks to #Bryan Oakley's answer I was able to code a working solution. I will share my code in the hope that it will help someone someday.
This example builds 2 tkinter TreeViews, and enables dragging-and-dropping treeItems between the 2 trees. The key point is that by binding both trees to the B1-motion event, both trees are able to respond to the events.
import tkinter as tk
from tkinter import ttk
from tkinter.messagebox import showinfo
class Singleton(type):
_instances = {}
def __call__(cls, *args, **kwargs):
if cls not in cls._instances:
cls._instances[cls] = super(Singleton, cls).__call__(*args, **kwargs)
return cls._instances[cls]
class TreeItem:
"""
Keeps a reference to a treeItem together with its parent tree.
"""
def __init__(self, tree, item):
self.tree = tree
self.item = item
self.itemTxt = tree.item(item,"text")
def __str__(self):
"""
Prints 'treename, itemname' upon calling str(TreeItem)
"""
return f'{self.tree}, {self.itemTxt}'
class Mouse(metaclass=Singleton):
"""
Handles treeitem clicking, dragging, dropping and shows feedback messages about them.
"""
def __init__(self, root):
self.root = root
self.clicked_item = None
self.is_dragging = False
self.drag_time = 0
self.current_hovering_widget = None
def OnMouseDown(self, event):
clicked_item = self.get_item_under_mouse(event)
print("You clicked on", str(clicked_item))
self.clicked_item = clicked_item
def OnDrag(self, event):
self.is_dragging = True
self.show_drag_init_msg()
self.show_hovering_item_change_msg(event)
self.drag_time += 1
def OnMouseUp(self, event):
if self.is_dragging:
self.finish_drag()
self.show_drop_msg()
self.clicked_item = None
def finish_drag(self):
self.is_dragging = False
self.drag_time = 0
def show_drag_init_msg(self):
if self.drag_time == 0:
print("You are now dragging item", self.clicked_item.tree, self.clicked_item.itemTxt)
def show_hovering_item_change_msg(self, event):
currently_hovering = self.get_item_under_mouse(event)
if str(self.current_hovering_widget) != str(currently_hovering):
print("Mouse is above", str(currently_hovering))
self.current_hovering_widget = currently_hovering
def show_drop_msg(self):
dragged_item:TreeItem = self.clicked_item
dragged_onto:TreeItem = self.current_hovering_widget
print(f'You dropped {str(dragged_item)} onto {str(dragged_onto)}')
def get_item_under_mouse(self, event):
current_tree = self.root.winfo_containing(event.x_root, event.y_root)
current_tree_item = current_tree.identify("item", event.x, event.y)
return TreeItem(tree=current_tree, item=current_tree_item)
class Tree:
def __init__(self, root, row, col):
self.root: tk.Tk = root
self.create_tree(root, row, col)
def OnDrag(self,event):
Mouse(self.root).OnDrag(event)
def OnMouseDown(self, event):
Mouse(self.root).OnMouseDown(event)
def OnMouseUp(self, event):
Mouse(self.root).OnMouseUp(event)
def create_tree(self, root, row, col):
self.tree = ttk.Treeview(root)
self.tree.heading('#0', text='Departments', anchor='w')
self.tree.grid(row=row, column=col, sticky='nsew')
self.add_dummy_data()
# add bindings
self.tree.bind("<ButtonPress-1>", self.OnMouseDown)
self.tree.bind("<ButtonRelease-1>", self.OnMouseUp)
self.tree.bind("<B1-Motion>", self.OnDrag)
def add_dummy_data(self):
# adding data
self.tree.insert('', tk.END, text='Administration', iid=0, open=False)
self.tree.insert('', tk.END, text='Logistics', iid=1, open=False)
self.tree.insert('', tk.END, text='Sales', iid=2, open=False)
self.tree.insert('', tk.END, text='Finance', iid=3, open=False)
self.tree.insert('', tk.END, text='IT', iid=4, open=False)
# adding children of first node
self.tree.insert('', tk.END, text='John Doe', iid=5, open=False)
self.tree.insert('', tk.END, text='Jane Doe', iid=6, open=False)
self.tree.move(5, 0, 0)
self.tree.move(6, 0, 1)
root = tk.Tk()
root.geometry('620x200')
# make two trees
tree1 = Tree(root,0,0)
tree2 = Tree(root,0,1)
# run the app
root.mainloop()

Categories