Get selected item in listbox and call another function storing the selected for it

前端 未结 3 1166
不知归路
不知归路 2020-12-19 10:53

I have a canvas that calls createCategoryMeny(x) when it is clicked.

This function just creates a Toplevel() window,

def cr         


        
3条回答
  •  Happy的楠姐
    2020-12-19 11:19

    For spyder and python 3.6 this following code worked.

    import tkinter as tk
    root = tk.Tk()
    root.geometry("612x417")
    root.title("change label on listbox selection")
    root.resizable(0,0)
    root.configure(background='lightgrey')
    
    
    #Show selected currency for from in label
    frmcur_text = tk.StringVar()
    frmcur = tk.Label(root, textvariable=frmcur_text, font="Helvetica 10 bold", anchor='w', background='lightgrey').place(x=195,y=50)
    
    def onselect(evt):
        # Note here that Tkinter passes an event object to onselect()
    
        w = evt.widget
        index = int(w.curselection()[0])
        value = w.get(index)
    #    print ('You selected item %d: "%s"' % (index, value))
        frmcur_text.set(value)
    
    #Create listboxes for xurrency selection
    listbox1 = tk.Listbox(root, font="Helvetica 11 bold", height=3, width=10)
    listbox2 = tk.Listbox(root, font="Helvetica 11 bold", height=3, width=10)
    listbox1.place(x=300,y=50)
    listbox2.place(x=300,y=125)
    
    
    for i in range(20):
        i = i + 1
        listbox1.insert(1, i)
        listbox2.insert(1, i)
    
    
    listbox1.bind('<>', onselect)    
    
    cs = listbox1.curselection()
    
    frmcur_text.set(cs)
    
    root.mainloop()
    

提交回复
热议问题