-1

I am working with tkinter, as I have my gui set up with and entry, label, and button. I'm 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.

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.

I've just started programming in python and practicing. I've 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 I'm 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.

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

  def btn_Clicked():
    x = txt.get()
    if x in d.keys():
        decision.configure(text = "Your value, " + "this is where I'm lost, I'd 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)

I've also tried something along the lines of

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

In that example I get the corresponding value but its only for the input that I've 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.

Jonathan Hall
  • 75,165
  • 16
  • 143
  • 189
  • Could you put in your actual code? `hand` is not declared anywhere. Is it supposed to be `dict`? (By the way, it is best not to use keywords like `dict` as variable names).. – Deepstop Aug 05 '19 at 11:26
  • My apologies. d is the dictionary variable. And hand.keys() should be replaced with d.keys() – David Arnold Aug 05 '19 at 11:48

3 Answers3

0

Since it's a dictionary, you can directly use get() to get the value of the key.

def btn_Clicked():
    x = txt.get()
    check_in_dict = dict.get(x)

    if check_in_dict:
        decision.configure(text = "Your value, " + str(check_in_dict))
    else:
        decision.configure(text = "No key found")
Black Mask
  • 387
  • 1
  • 3
  • 16
  • 2
    This will raise an exception if the key is not on the dictionary. Consider using `dict.get(x)` instead as `__getitem__(key)` is just a different way of doing `dict[key]` – Gonzalo Hernandez Aug 05 '19 at 11:45
  • Agree `d.get(x)` will return `None` if not found, so the result can be checked in the subsequent `if` statement, with the "No key found" error error returned if there is no value. – Deepstop Aug 05 '19 at 11:52
  • Yes, I do agree with you. I have changed the code above. – Black Mask Aug 05 '19 at 12:18
  • This worked beautifully, thanks so much. Like I said new to coding/ stack overflow in general. Great experience and thanks for everyone's input. – David Arnold Aug 05 '19 at 12:57
0
dictionary = {"AA":1, "BB":2, "CC":3}

Below code will go into the button pressed

key = input("the key") # key is assumed to be input here

try:
    value = dictionary[key] # user entered key

    # do what you want with key and its value
    decision.configure(text = "Your value, " + value)

# if key not found in dict it would raise KeyError
except KeyError:
    # key not found message goes here
    decision.configure(text = "No key found")
Mensch
  • 670
  • 7
  • 16
0

Use the get method, which returns None if the key isn't found.

v = d.get(x)
if x:
    decision.configure(text = f"Your value, {v}")
else:
    decision.configure(text = f"No key found for {x}")
Deepstop
  • 3,627
  • 2
  • 8
  • 21