lundi 5 août 2019

Using a users input to search dictionary keys and print that keys matching value

Working with tkinter, as I have my gui set up with and entry, label, and button. Im trying to search my dictionary's keys with the users input from the entry, and print the value of the key that was typed.

ex. d = {"A":1, "B":2, "C":3}

user types B into the entry presses the button and if input == "B" then print 2 to the label, else print "does not match"

That's the idea at least.

currently I can see if the users input is in the dictionary and print some string to the label but not the value of the key that was typed into the entry.

Now please bare with me as this is my first post. Ive just started programming in python and practicing. Ive searched for about two days on this issue and can only find for loops that are basically skipping over the if statement and going right to the else. Or if the entry is "A" It prints value 3. Which I assume is some kind of reversed dictionary. so I tried to figure it out myself. If im on the right track that would be great to here lol but if Im just completely wrong well..

so I've tried a normal if else statement, a for loop, and using methods for a dictionary.

dict = {"AA":1, "BB":2, "CC":3}

  def btn_Clicked():
    x = txt.get()
    if x in hand.keys():
        decision.configure(text = "Your value, " + "this is where Im lost, Id like it to print the value of that specific key)
    else:
        decision.configure(text = "No key found")


btn = ttk.Button(win, text = "Find value", command = btn_clicked)
btn.grid(column = 2, row = 0)


txt = ttk.Entry(win, width = 10)
txt.grid(column = 1, row = 0)

position_entry = ttk.Label(win, text= "Enter Key", font = ("Arial Bold", 12))
position_entry.grid(column= 0, row = 0 )

decision = ttk.Label(win, text = "", font = ("Arial Bold", 10))
decision.grid(column= 0,row = 1)

Ive also tried something along the lines of

 if txt.get() == list(hand.keys())[0]:
          decision.configure(text = "Your Value is " + str(list(hand.values())[0])

In that example I get the corresponding value but its only for the input that Ive entered, [0], [1], ect for items in the dictionary.

No error messages, just not doing what I want it to. if entry == to a key in dictionary then print "message" + that keys value to a label.

Aucun commentaire:

Enregistrer un commentaire