How to remove multiple selections from a listbox - python

I got this code that put in the variable ativo what is selected in the listbox
def selecionado(evt):
global ativo
a=evt.widget
b=a.curselection()
if len(b) > 0:
ativo=[a.get(i) for i in a.curselection()]
else:
ativo=[]
How can i create a function that can delete this, because ativo return this:
["Name1\t Code1\n"]
["Name1\t Code1\n", 'Name2\t Code2\n']
But if i print the listbox, this shows up
.59070320.59070384
Even though the listbox is present in my graphic interface with the names and correspodent codes the right way, like:
Name Code
Name Code
So, i have no idea how to delete what is selected, please help me

Just use yourlistboxname.delete(index)
def removeclicked(evt):
w = evt.widget
index = int(w.curselection()[0])
label = w.get(index)
yourlistboxname.delete(index)
To bind the evt, add this where you create the widget
yourlistboxname.bind('<<ListboxSelect>>',removeclicked)
This solution is very easy to find, so maybe I am mistaking your problem. you don't remove the entries in your code, you only get() the entries or change them.
This page should help:
http://effbot.org/tkinterbook/entry.htm
http://effbot.org/tkinterbook/tkinter-events-and-bindings.htm

Related

How to a convert a listbox item's name to a label in Python

For the program that I am required to write up, I need the name of a Listbox item to also be the text in the label. This is the code that I thought would work, but it only configurated the label to be the item's number in the Listbox, not the name:
def openAccount(self):
selectedItem = ltboxSrcUser.curselection()
for item in selectedItem:
rootMainPage.withdraw()
rootOthAccPage.deiconify()
lblOtherUserName.config(text = ltboxSrcUser.curselection())
Can anybody help me out?
The documentation for the listbox shows that curselection returns the index of the selected items, not the values. To get the values you must pass the index to the get method.
def openAccount(self):
selectedItems = ltboxSrcUser.curselection()
if selectedItems:
rootMainPage.withdraw()
rootOthAccPage.deiconify()
index = selectedItems[0]
item = ltboxSrcUser.get(index)
lblOtherUserName.config(text=item)

How to make each radiobutton unique from the others in tkinter

I have a window in Tkinter that looks like this:
When i click on a button in the first row, it stays. However, when i click on a button in the second row, it unselects the one i chose above.
I want it to be able to only select one option per row. Is there something I'm missing? When it's done, I want to be able to iterate over the rows and get the value of the boxes, but I'm not sure how to do that either.
The code for that section is:
for i in studentList:
Label(left,text=i[0][::]+' ' + i[1][::],fg='black',bg='#dbdbdb',font=('Arial',11,'bold')).grid(row=counter,column=0,pady=13,sticky='news')
P = Radiobutton(right,text='Present',bg='#56ab32',fg='black',value='P'+str(counter),indicatoron = 0,font=('Arial',12,'bold'))
P.grid(row=counter,column=0,pady=10,padx=20,sticky='news')
L = Radiobutton(right,text='Leave',bg='#e6a800',fg='white',indicatoron = 0,value='L'+str(counter),font=('Arial',12,'bold'))
L.grid(row=counter,column=1,pady=10,padx=20,sticky='news')
Radiobutton(right,text='Absent',bg='#bd2900',fg='white',indicatoron = 0,value='A'+str(counter),font=('Arial',12,'bold')).grid(row=counter,column=2,pady=10,padx=20,sticky='news')
counter+=1
Radiobuttons work by assigning two or more radiobuttons the same instance of one of tkinter's special variable objects -- usuallyStringVar or IntVar. This sharing of a variable is what makes a group of radiobuttons work as a set, since the variable can only hold a single value.
Because you aren't assigning a variable, tkinter is using a default variable which is the same for every button. Thus, all buttons are acting as a single set.
To make your code work, each row needs to use it's own instance of StringVar. It would look something like this:
vars = []
for i in studentList:
var = StringVar()
vars.append(var)
...
Radiobutton(right, variable=var, ...)
Radiobutton(right, variable=var, ...)
Radiobutton(right, variable=var, ...)
...
With the above, you can get the choice of each row by getting the value of the variable for that row. For example, the first row would be vars[0].get(), the second row would be vars[1].get() and so on.

How to remove multiple selected items in listbox in tkinter Python?

I want to be able to delete all selected items when I use selectmode=MULTIPLE.
I have tried to delete but it only deletes the item that was selected last. Is there any way to delete all of the items.
Thanks
from tkinter import *
def abc():
listbox.delete(ACTIVE)
def xyz():
z=listbox.get(0, END)
print (z)
master = Tk()
scrollbar = Scrollbar(master,orient=VERTICAL)
listbox = Listbox(master, yscrollcommand=scrollbar.set, selectmode=MULTIPLE)
scrollbar.config(command=listbox.yview)
b = Button(master, text="delete", command=abc)
b.pack(side=RIGHT)
b2 = Button(master, text="save", command=xyz)
b2.pack(side=RIGHT)
scrollbar.pack(side= RIGHT, fill=Y)
listbox.pack(side=LEFT)
for item in ["one", "two", "three", "four", "five"]:
listbox.insert(END, item)
mainloop()
To get all selected items instead of only the last one, you can use listbox.curselection() and then delete them one by one, starting from the last one so that the indexes of the other ones are not modified by the deletion.
def abc():
sel = listbox.curselection()
for index in sel[::-1]:
listbox.delete(index)
By the way, I advise you to give meaningful names to your functions (like "delete" instead of "abc").
Only half of selected items are deleted.
I think the index is recreated after each delete so items reassigned to a deleted index will not be deleted.
item index: 0,1,2,3 after deleting 0 becomes 0,1,2 then it thinks that 0 is already deleted so deletes 1. After deleting 1 the index is 0,1 - which are both already deleted so nothing more to delete.
ANSWER: The trick is to delete the selected items in reverse order so that items earlier in the list are not reindexed by delete:
def call_delete():
selection = listBox.curselection()
for i in reversed(selection):
listBox.delete(i)
EDIT The better answer is not this one, but leaving this here for the documentation and the url. Check out https://stackoverflow.com/a/44818820/1141389
Take a look here for more info on Listbox wiget
You can clear the whole listbox with
listbox.delete(0, END)
If you only want the selected items deleted, I think you could do something like the following:
def abc():
items = map(int, listbox.curselection())
for item in items:
listbox.delete(item)
but note that I cannot test this currently. Try the above and checkout the website, that should get you on the right track.

How to get a current Item's info from QtGui.QListWidget?

Created a QtGui.QListWidget list widget:
myListWidget = QtGui.QListWidget()
Populated this ListWidget with QListWidgetItem list items:
for word in ['cat', 'dog', 'bird']:
list_item = QtGui.QListWidgetItem(word, myListWidget)
Now connect a function on list_item's left click:
def print_info():
print myListWidget.currentItem().text()
myListWidget.currentItemChanged.connect(print_info)
As you see from my code all I am getting on a left click is a list_item's label name. But aside from a label name I would like to get a list_item's index number (order number as it is displayed in ListWidget). I would like to get as much info on left-clicked list_item as possible. I looked at dir(my_list_item). But I can't anything useful there ( other than already used my_list_item.text() method which returns a list_item's label name). Thanks in advance!
Use QListWidget.currentRow to get the index of the current item:
def print_info():
print myListWidget.currentRow()
print myListWidget.currentItem().text()
A QListWidgetItem does not know its own index: it's up to the list-widget to manage that.
You should also note that currentItemChanged sends the current and previous items as arguments, so you could simplify to:
def print_info(current, previous):
print myListWidget.currentRow()
print current.text()
print current.isSelected()
...
Well, I have listed some of the things you can display about the current item, if you want more than this then you should look through the PyQt Documentation. link
def print_info():
print myListWidget.currentItem().text()
print myListWidget.row(myListWidget.currentItem())
print myListWidget.checkState() # if it is a checkable item
print myListWidget.currentItem().toolTip().toString()
print myListWidget.currentItem().whatsThis().toString()
myListWidget.currentItemChanged.connect(print_info)

Change Listbox items based on selection in another Listbox

I am trying to dynamically change the entries in a Listbox (lb_ledgers) depending on the selection in another Listbox (lb_book). I currently have the following lines of code:
lb_book.bind('<<ListboxSelect>>', onselect)
def onselect(evt):
w = evt.widget
index = int(w.curselection()[0])
value = w.get(index)
The problem is that I do not know how to pass the first Listbox (lb_ledgers) to the function onselect in order to change its contents. I have tried using a lambda construct, but that did not work. Could you please let me know how to solve this?
Thanks,
MiddleClassMan

Categories

Resources