Change Listbox items based on selection in another Listbox - python

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

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 get the value of radiobutton in Tkinter?

I'd like to get the value as text and not integer on my raddiobutton.
I have a frame, named "printers_frame" in which I created radio buttons with a list of printers on the computer of the user.
According to the printer choosen (radiobutton), I will print files on this specific printer.
I followed this tips but I want to do the same with String, not Integer.
Here is the code to create the radio button:
print_variable = StringVar()
for p in printers:
Radiobutton(printers_frame, text = p[2], variable = print_variable, value = p[2], wraplength=int(WIDTH/3)-10, justify="left", bg=BACKGROUND).pack(side = TOP, anchor = W)
Can you help?
Thanks a lot!
Can't you just take the integer value, and convert it with str()?
You can save that in a variable, and use it however you want.
Hopefully, this helps.
Edit:
Say the value of the RadioButton is RadioButton().
You can just do str(RadioButton).
Edit: You can use the StrVar() function in tkinter. You assign your Radio Button to that variable, and then you can use the value as a string.
Then pass the variable with the string when you are initializing your Radio Button. Pass it to a parameter called variable.
So, your radio button would be:
radio_variable = IntVar()
button = RadioButton(master, ..., variable = radio_variable)
Hopefully this helps!

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 selections from a listbox

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

Set OptionMenu by index

I'm trying to set the value in an OptionMenu by the index of the options, and not use the actual option value itself.
For example, if my OptionMenu has options ["one", "two", "three"], I want to be able to set it to "one" by writing My_Option_Menu.set(options[0]), not My_Option_Menu.set("one")
The OptionMenu is dynamically populated by a list depending on what two other OptionMenus are set to, which is why I want to refer to the options by index and not actual value. Does the OptionMenu object have a way to return the list of options that are currently populated so that my options[0] method above will work?
The simplest way to do this is to keep a copy of the list used to define the optionmenu. Then you can use the index to get the value from the list and assign it to the associated variable.
For example:
options = ["one","two","three"]
...
self.menuVar = tk.StringVar(master)
self.optionMenu = tk.OptionMenu(master, self.menuVar, *options)
...
self.menuVar.set(options[0])
Another option is to get the menu object associate with the widget, and use menu commands to get the text at the given index, and use that to set the variable:
value = self.optionMenu["menu"].entrycget(0, "label")
self.menuVar.set(value)

Categories

Resources