1

How to use tkinter listbox to the calculation... The user can make a choice in two listbox ,Function should be taken from the upper listbox dic key And lower listbox dic value.... I have a code like this:(But it does n't work)

from tkinter import *
from tkinter import ttk

def calculate():
    a = int(l1.curselection())
    b = int(si1.curselection())
    t = a+b
    t=Label(aken,text="answer: %.2f" % tulemus).grid(row=7,column=5)
    return(t)




aken = Tk()
aken.title('kalk')
aken.configure(background='#E6F3FE')
aken.geometry("420x200")




l1 = ttk.Label(aken, text="2 ", background="#E6F3FE")
l1.grid(column=0, row=3, padx=5, pady=5, sticky=(N, W, ))

l1 = Listbox(aken, height=1)
pr = {50:60,80:46}
for i in pr:
    l1.insert(END, i)
    l1.grid(column=1, row=3, padx=5, pady=5, sticky=(N, W, E))

si1 = ttk.Label(aken, text="1 ", background="#E6F3FE")
si1.grid(column=0, row=4, padx=5, pady=5, sticky=(N, W, ))

si1 = Listbox(aken, height=1)
pr = {50:60,80:46,8:6}
for i in pr:
    si1.insert(END, i)
    si1.grid(column=1, row=4, padx=5, pady=5, sticky=(N, W, E))



nupp = ttk.Button(aken, text="Calculate", command=calculate)
nupp.grid(column=1, row=7, padx=5, pady=5, sticky=(N, S, W, E))


aken.mainloop()
Janek Lass
  • 37
  • 1
  • 5

1 Answers1

0

Main problem here - your calculate function. You got error here because curselection() returns a tuple. Of course, you can use curselection()[0], but there're can be no selection and each time you need to convert a string to an integer.

So we can workaround it with get('active') and since your dict full of integers - we're free from conversion:

def calculate():
    a = l1.get('active')
    b = si1.get('active')
    tulemus = a+b
    Label(aken, text="answer: %.2f" % tulemus).grid(row=7, column=5)

Another weak spot - grid inside a loop. Feel free to move grid around! So now your code "works" or "produce no error":

from tkinter import *
from tkinter import ttk


def calculate():
    a = l1.get('active')
    b = si1.get('active')
    tulemus = a+b
    Label(aken, text="answer: %.2f" % tulemus).grid(row=7, column=5)

aken = Tk()
aken.title('kalk')
aken.configure(background='#E6F3FE')
aken.geometry("420x200")

l1 = ttk.Label(aken, text="2 ", background="#E6F3FE")
l1.grid(column=0, row=3, padx=5, pady=5, sticky=(N, W, ))

l1 = Listbox(aken, height=1)
l1.grid(column=1, row=3, padx=5, pady=5, sticky=(N, W, E))

pr = {50: 60, 80: 46}
for i in pr:
    l1.insert(END, i)

si1 = ttk.Label(aken, text="1 ", background="#E6F3FE")
si1.grid(column=0, row=4, padx=5, pady=5, sticky=(N, W, ))

si1 = Listbox(aken, height=1)
si1.grid(column=1, row=4, padx=5, pady=5, sticky=(N, W, E))

pr = {50: 60, 80: 46, 8: 6}
for i in pr:
    si1.insert(END, i)

nupp = ttk.Button(aken, text="Calculate", command=calculate)
nupp.grid(column=1, row=7, padx=5, pady=5, sticky=(N, S, W, E))

aken.mainloop()

Also in each loop you iterate over dict keys, hence you listboxes are both populated with keys. Your idea isn't clear to me, but if you need values in lower listbox - use this loop (iterating over dict):

for i in pr.values():
    si1.insert(END, i)

Also:

Community
  • 1
  • 1
CommonSense
  • 4,232
  • 2
  • 14
  • 38
  • I have one more problem ,If I want the user to see key ,but ,function use value... How i to do it? – Janek Lass May 18 '17 at 18:57
  • @JanekLass, it's up to you. And it's better to create new question with your ideas and desired behaviour. But idea is simple - track selection changes of desired listbox. – CommonSense May 19 '17 at 03:59