I need to store items in a Gtk TreeView and when interacting with this TreeView, the user will can select one or more items in the list.
Because I'm new to GTK, I managed to populate the treeview and display a checkbox as the code below shows. But when I try to select, nothing happens and I do not know how to make this possible.
This is my Code:
# the column is created
renderer_products = gtk.CellRendererText()
column_products = gtk.TreeViewColumn("Products", renderer_products, text=0)
# and it is appended to the treeview
view.append_column(column_products)
# the column checkbox is created
renderer_checkbox = gtk.CellRendererToggle()
column_checkbox = gtk.TreeViewColumn("Selected", renderer_checkbox, text=0)
# and it is appended to the treeview
view.append_column(column_checkbox)
If you want to select the whole row and something happen:
#double click or not double click use
Gtk.TreeView.set_activate_on_single_click (bool)
#connect the treeview
treeview.connect ("row-activated", on_row_activate)
#inside the callback
def on_row_activate (treeview, path, column):
model = treeview.get_model ()
iter = treeview.get_iter (path)
yourdata = model[iter][model_index]
#do whatever with yourdata
If you want when you click the toggle and something happen:
#connect the renderer
renderer_checkbox.connect ("toggled", on_selected_toggled)
#inside the callback
def on_selected_toggled (renderer, path):
#modify the model or get the value or whatever
Related
im trying to create an UI with PyQt5 which has a tableWidget and a label that will display the text in every 4th column of the table, by order while the user scrolls through.
I cant seem to get the text in the selected cell from the table.. closest i got is this:
def open_csv_in_table (self):
f = open ("test.csv")
fData = csv.reader(f)
csvTable = list(fData)
self.tableWidget.setRowCount(len(csvTable))
self.tableWidget.setColumnCount(len(csvTable[0])-4)
for line in range( len(csvTable)):
for row in range(len(csvTable[0])):
self.tableWidget.setItem(line, row,QtWidgets.QTableWidgetItem(csvTable[line][row]))
self.tableWidget.setColumnWidth(0 , 10) # ID
self.tableWidget.setColumnWidth(1 , 150) # TEST NAME
self.tableWidget.setColumnWidth(2 , 50) # STATUS
self.tableWidget.setColumnWidth(3 , 300) # REMARKS
self.tableWidget.setColumnWidth(4 , 737) # LONG DESCRIPTION
def label_display(self):
self.label.setText(str(self.tableWidget.itemClicked))
print(str(self.tableWidget.itemClicked))
And im calling the display function with:
self.open_csv_in_table()
self.tableWidget.itemClicked.connect (lambda: self.label_display())
itemClicked is a signal that does not have the information of the item clicked so that is not the way to get that value. The signals what to do is pass the data through the arguments of the slot, in your case you must change to:
def label_display(self, item):
self.label.setText(item.text())
and
self.open_csv_in_table()
self.tableWidget.itemClicked.connect(self.label_display)
So I'm grabbing links of events off a website and putting them into a drop down menu to be selected. My code for the menu:
import Tkinter as tk
from Tkinter import StringVar
selectMenu = tk.Tk()
# #-> this is what I have
# Followed by what you can use
#var = Vars()
#events = var.GetVars('Event')
events = " "
options = []
links = []
#forms = (driver.find_elements_by_class_name("with-cats")) #This is what I have
forms = ["Yolo ","Dad? Closed","Anotha One","Normies! Closed"] #This is so you can try it for yourself
for x in forms:
#info = x.text
info = x #Again, this is so you can try it for yourself
if events in info.lower():
links.append(x)
for link in range(0,len(links)):
#options.append(links[link].text)
options.append(links[link])
list(set(options))
selection = []
for link in range(0,len(options)):
selection.append(options[link])
select = StringVar(selectMenu)
select.set("--None Selected--")
menu = tk.OptionMenu(selectMenu, select, *(selection))
msg = "Which one would you like to attend?"
label = tk.Label(selectMenu, text=msg, font="Helvedica 14")
label.pack(side='top', pady=10)
menu.pack(side="top", pady=10)
selectMenu.attributes('-topmost', True)
selectMenu.mainloop()
So this works fine and dandy, but I would like to improve the look to make it more obvious which events are open. To clarify, an event found that is open and put into the menu may look like "This is a cool event", but one that is closed would be read as "This is a cool event Closed". My aim is to be able to make the foreground red of either just the word Closed or the string containing Closed, whichever is possible if any (And I'm not sure if it's possible because menus and buttons on osx are usually defaulted to system settings, maybe there is a way around this?).
Current: Desired:
According to the documentation for OptionMenu here and here I don't think there is a way to set the color of text.
You might be able to get something close to what you want by using a listBox instead. See post here for the listBox example.
Found a solution! Using a Menu inside of a MenuButton the same way Tkinter creates MenuOptions, I was able to create a custom MenuOption. If you want to add more options, you can use the menbutton.configure() option to edit the button, and menbutton.menu to edit the menu items.
import Tkinter as tk
from Tkinter import Menu, Menubutton
class Vars():
global vari
vari = {}
def GetVars(self, var):
return vari.get(str(var))
def SendVars(self, var, val):
vari[str(var)] = val
class App():
def buttselect(self, link, menbutton, selectMenu):
var = Vars()
var.SendVars("Selection", link) # Store selected event
menbutton.configure(text=link) # Set menu text to the selected event
def prnt(self, link):
var = Vars()
print var.GetVars("Selection") # Print event
def __init__(self, selectMenu):
events = " "
options = []
links = []
forms = ["Yolo ","Dad? Closed","Anotha One","Normies! Closed"] #This is so you can try it for yourself
menbutton = Menubutton (selectMenu, text="--None Selected--", relief="raised")
menbutton.grid()
menbutton.menu = Menu (menbutton, tearoff=0)
menbutton["menu"] = menbutton.menu
#Get a list of event names
for x in forms:
info = x #Again, this is so you can try it for yourself
#If desired event keyword is in an event name, add it to the correct links
if events in info.lower():
links.append(x)
#Remove duplicates
for link in range(0,len(links)):
options.append(links[link])
list(set(options))
#Final list of event names turned into menu commands
for link in options:
if "Closed" in link:
menbutton.menu.add_command( label= link, command= lambda link=link: self.buttselect(link, menbutton, selectMenu), foreground='red')
else:
menbutton.menu.add_command( label= link, command= lambda link=link: self.buttselect(link, menbutton, selectMenu))
b = tk.Button(selectMenu, text="Selection", command= lambda link=link: self.prnt(link)) #Print selected event
b.pack()
msg = "Which one would you like to attend?"
label = tk.Label(selectMenu, text=msg, font="Helvedica 14")
label.pack(side='top', pady=10)
menbutton.pack(side="top", pady=10)
selectMenu = tk.Tk()
selectMenu.attributes('-topmost', True)
app = App(selectMenu)
selectMenu.mainloop()
This results in exactly the result desired:
I found a way!
Let's say x is an optionmenu with options:
options=['Red','Blue','Green']
defopt=tk.StringVar(options[0]) #StringVariable to hold the selected option.
x=tk.OptionMenu(self.optmenuframe,defopt,*options)
Now, get the menu object from the optionmenu and use entryconfig method. That's it!
x.children['menu'].entryconfig(0,foreground='red')
x.children['menu'].entryconfig(1,foreground='blue')
x.children['menu'].entryconfig(2,foreground='green')
#0 is the index of the option you want to apply the configurations to.
I am very new to PyQt, so I am not even sure where to start searching for this.
So I have two different options for QRadioButtons which ideally will correspond to two QPushButtons, one each.
Basically, I have the following code, where i tried to achieve this by using if statements:
def tab1UI(self):
mytabfont = QFont('Lucida Sans Unicode', 9)
layout = QFormLayout()
#self.setTabText(0,"My Data")
self.tab1.setLayout(layout)
tabdescription = 'To obtain or generate data choose an option below:'
# radio options
label1 = QLabel(tabdescription)
label1.setFont(mytabfont)
layout.addWidget(label1)
radiobtn1 = QRadioButton('Load data from file')
radiobtn1.setChecked(True)
#why does my resize not work?
radiobtn1.resize(100,100)
radiobtn1.setFont(mytabfont)
layout.addWidget(radiobtn1)
loadbtn = QPushButton('Open CSV file...')
layout.addWidget(loadbtn)
radiobtn2 = QRadioButton('Generate data')
radiobtn2.setFont(mytabfont)
genbtn= QPushButton('Generating matrix...')
layout.addWidget(radiobtn2)
layout.addWidget(genbtn)
if radiobtn1.isChecked():
# if this option is clicked then this button needs to be activated else it must be de-activated
loadbtn.setEnabled(True)
genbtn.setEnabled(False)
elif radiobtn2.isChecked():
loadbtn.setEnabled(False)
genbtn.setEnabled(True)
else:
loadbtn.setEnabled(False)
genbtn.setEnabled(False)
So, whenever I click one radio-button option I would like one pushbutton to become automatically active or inactive when the other option is checked instead.
There must be some sort of Action to be connected but not sure how to go about this.
You're only running the if statement once, when the buttons are first created. In order for this to work, you need to evaluate those if statements every time the radio button check state is changed. Qt allows you to do this with Signals and Slots. The QRadioButton will emit a signal when you change the check state. You can connect to this signal and run a function that updates the enabled state of the other buttons.
def tab1UI(self):
mytabfont = QFont('Lucida Sans Unicode', 9)
layout = QFormLayout()
self.tab1.setLayout(layout)
tabdescription = 'To obtain or generate data choose an option below:'
# radio options
self.label1 = QLabel(tabdescription)
self.label1.setFont(mytabfont)
layout.addWidget(self.label1)
self.radiobtn1 = QRadioButton('Load data from file')
self.radiobtn1.setChecked(True)
self.radiobtn1.setFont(mytabfont)
layout.addWidget(self.radiobtn1)
self.loadbtn = QPushButton('Open CSV file...')
layout.addWidget(self.loadbtn)
self.radiobtn2 = QRadioButton('Generate data')
self.radiobtn2.setFont(mytabfont)
self.genbtn= QPushButton('Generating matrix...')
layout.addWidget(self.radiobtn2)
layout.addWidget(self.genbtn)
self.radiobtn1.toggled.connect(self.refresh_button_state)
self.radiobtn2.toggled.connect(self.refresh_button_state)
self.refresh_button_state()
def refresh_button_state(self):
if self.radiobtn1.isChecked():
self.loadbtn.setEnabled(True)
self.genbtn.setEnabled(False)
elif self.radiobtn2.isChecked():
self.loadbtn.setEnabled(False)
self.genbtn.setEnabled(True)
else:
self.loadbtn.setEnabled(False)
self.genbtn.setEnabled(False)
I want to implement a feature which will allow user to navigate in Gtk.TreeView widget by arrow keys, unfortunately select_iter() method is not doing what I was expecting from it, i. e. it fails to select parent node of selected node :P
And now I need explanation why it's not working or hint on some kind of workaround of this issue.
Below is ready to run test program which demonstrates this problem. Problematic line of code is tagged with #FIXME.
from gi.repository import Gtk
from gi.repository import Gdk
class WizardManager(Gtk.Dialog):
'''Dialog window which makes possible to choose type of resource to create by editor.'''
def __init__(self, parent):
super().__init__('Wizard manager', parent, Gtk.DialogFlags.MODAL | Gtk.DialogFlags.DESTROY_WITH_PARENT)
self.set_default_response(Gtk.ResponseType.OK)
self.set_decorated(False)
self.set_size_request(640, 480)
vbox = self.get_content_area()
self.__tree_store = Gtk.TreeStore(str)
self.__tree_view = Gtk.TreeView(self.__tree_store)
self.__tree_view.get_selection().set_mode(Gtk.SelectionMode.SINGLE)
self.__tree_view.connect('key-press-event', self.__on_tree_view_key_press)
self.__tree_view.set_headers_visible(False)
text_renderer = Gtk.CellRendererText()
text_column1 = Gtk.TreeViewColumn(None, text_renderer)
text_column1.add_attribute(text_renderer, 'text', 0)
self.__tree_view.append_column(text_column1)
scrolled_window = Gtk.ScrolledWindow()
scrolled_window.add(self.__tree_view)
vbox.pack_start(scrolled_window, True, True, 0)
self.__populate_tree_store()
self.show_all()
def __on_tree_view_key_press(self, tree_view, event):
# TODO Implement tree navigation with arrow keys
tree_selection = tree_view.get_selection()
selected_iter = tree_selection.get_selected()[1]
if selected_iter:
selected_tree_path = self.__tree_store.get_path(selected_iter)
# Right arrow and Return should expand selected node.
if event.keyval == Gdk.KEY_Right or event.keyval == Gdk.KEY_Return:
tree_view.expand_row(selected_tree_path, False)
# Left arrow should collapse node or select it parent.
elif event.keyval == Gdk.KEY_Left:
if not tree_view.collapse_row(selected_tree_path):
# Unable to collapse node it must be empty. select it's parent.
parent_iter = selected_iter.copy()
if self.__tree_store.iter_parent(parent_iter):
# FIXME Why select_iter() executes without error and is not able to select parent node?
# same goes for select_path() :P
tree_selection.select_iter(parent_iter)
def __populate_tree_store(self):
# Ordinary resources
self.__tree_store.append(None, ('File',))
self.__tree_store.append(None, ('Directory',))
# Python files
python_dir = self.__tree_store.append(None, ('Python',))
self.__tree_store.append(python_dir, ('Python module',))
self.__tree_store.append(python_dir, ('Python package',))
# Django files
django_dir = self.__tree_store.append(python_dir, ('Django',))
self.__tree_store.append(django_dir, ('Django project',))
self.__tree_store.append(django_dir, ('Django app',))
if __name__ == '__main__':
app = Gtk.Window(Gtk.WindowType.TOPLEVEL)
app.connect('destroy', lambda a: Gtk.main_quit())
dlg = WizardManager(app)
dlg.run()
dlg.destroy()
Gtk.main()
Here you have a hint!
#! /usr/bin/python
###########################################################
#
# Basic Gtk.TreeView Example with two sortable columns
#
###########################################################
# use the new PyGObject binding
from gi.repository import Gtk
import os
import getpass # this is only to automatically print your home folder.
class MyWindow(Gtk.Window):
def __init__(self):
Gtk.Window.__init__(self, title='My Window Title')
self.connect('delete-event', Gtk.main_quit)
# Gtk.ListStore will hold data for the TreeView
# Only the first two columns will be displayed
# The third one is for sorting file sizes as numbers
store = Gtk.ListStore(str, str, long)
# Get the data - see below
self.populate_store(store)
treeview = Gtk.TreeView(model=store)
# The first TreeView column displays the data from
# the first ListStore column (text=0), which contains
# file names
renderer_1 = Gtk.CellRendererText()
column_1 = Gtk.TreeViewColumn('File Name', renderer_1, text=0)
# Calling set_sort_column_id makes the treeViewColumn sortable
# by clicking on its header. The column is sorted by
# the ListStore column index passed to it
# (in this case 0 - the first ListStore column)
column_1.set_sort_column_id(0)
treeview.append_column(column_1)
# xalign=1 right-aligns the file sizes in the second column
renderer_2 = Gtk.CellRendererText(xalign=1)
# text=1 pulls the data from the second ListStore column
# which contains filesizes in bytes formatted as strings
# with thousand separators
column_2 = Gtk.TreeViewColumn('Size in bytes', renderer_2, text=1)
# Mak the Treeview column sortable by the third ListStore column
# which contains the actual file sizes
column_2.set_sort_column_id(1)
treeview.append_column(column_2)
# Use ScrolledWindow to make the TreeView scrollable
# Otherwise the TreeView would expand to show all items
# Only allow vertical scrollbar
scrolled_window = Gtk.ScrolledWindow()
scrolled_window.set_policy(
Gtk.PolicyType.NEVER, Gtk.PolicyType.AUTOMATIC)
scrolled_window.add(treeview)
scrolled_window.set_min_content_height(200)
self.add(scrolled_window)
self.show_all()
def populate_store(self, store):
directory = '/home/'+getpass.getuser()
for filename in os.listdir(directory):
size = os.path.getsize(os.path.join(directory, filename))
# the second element is displayed in the second TreeView column
# but that column is sorted by the third element
# so the file sizes are sorted as numbers, not as strings
store.append([filename, '{0:,}'.format(size), size])
# The main part:
win = MyWindow()
Gtk.main()
I have the following code in pygtk:
....
rendererText = gtk.CellRendererText()
self.columns["hour"] = gtk.TreeViewColumn("Uur", rendererText, text=0)
self.columns["hour"].set_sort_column_id(0)
self.treeview.append_column(self.columns["hour"])
self.columnControls["ond"] = gtk.CellRendererToggle()
self.columns["ond"] = gtk.TreeViewColumn("ond", self.columnControls["ond"], active=1)
self.columns["ond"].set_sort_column_id(1)
self.treeview.append_column(self.columns["ond"])
....
So, I'd personally expect that if I click the checkbox that appears in the column entrys would be "togglable", but it isn't. Is this because of my OS (Mac OS X 10.8), or is there some kind of property I forgot to set?
You have to bind a callback function, which may reverse the value in that GtkListStore/GtkTreeStore, to toggled signal. Like this:
def on_cellrenderertoggle_toggled(self, cellrenderertoggle, path):
# self.liststore is the tree modal of that treeview
self.liststore[path][1] = not self.liststore[path][1]
A full tutorial can be found here: http://python-gtk-3-tutorial.readthedocs.org/en/latest/cellrenderers.html#cellrenderertoggle