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

2024/10/8 2:29:03

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.

Answer

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")
https://en.xdnf.cn/q/118745.html

Related Q&A

How to write CSV into the next column

I have output that I can write into a CSV. However, because of how i setup my XML to text, the output iterates itself incorrectly. Ive tried a lot to fix my XML output, but I dont see any way to fix it…

Comparing date from pandas dataframe to current date

Im currently trying to write a script that does a specific action on a certain day. So for example, if today is the 6/30/2019 and in my dataframe there is a 6/30/2019 entry, xyz proceeds to happen. How…

How to divide a binary file to 6-byte blocks in C++ or Python with fast speed? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.Want to improve this question? Update the question so it focuses on one problem only by editing this post.Closed 5…

Selenium, python dynamic table

Im creating a robot with selenium that get all info from agencies in Brasil, ive alredy done the permutation click between all States and counties, all i have to do nows click in all agencies and get i…

How to split string into column

I got a csv file with some data, and I want to split this data.My column one contains a title, my column 2 contains some dates, and my column 3 contains some text linked to the dates.I want to tran…

remove whitespaces with new line characters

I have a string that looks like that:"\n My name is John\n and I like to go.\n blahblahblah.\n \n\n ".Note - In this string example, there are 5 white-spaces after…

Python tkinter GUI freezing/crashing

from Tkinter import * import tkFileDialog import tkMessageBox import os import ttkimport serial import timeit import time################################################################################…

If/else in python list comprehension

I would like to return random word from file, based on passed argument. But if the argument doesnt match anythning I dont want to return anything. My method looks like:def word_from_score(self,score):p…

Conditional module importing in Python

Im just trying out Maya 2017 and seen that theyve gone over to PySide2, which is great but all of my tools have import PySide or from PySide import ... in them.The obvious solution would be to find/rep…

AttributeError: list object has no attribute lower : clustering

Im trying to do a clustering. Im doing with pandas and sklearn. import pandas import pprint import pandas as pd from sklearn.cluster import KMeans from sklearn.metrics import adjusted_rand_score from s…