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.