使用用户输入来搜索字典键并打印与值匹配的键 [英] Using a users input to search dictionary keys and print that keys matching value

查看:63
本文介绍了使用用户输入来搜索字典键并打印与值匹配的键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用tkinter,因为我已经设置了gui以及输入,标签和按钮.我正在尝试使用条目中用户的输入来搜索字典的键,并打印键入的键的值.例如.

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}

用户B输入条目时,按下按钮,如果输入=="B",则在标签上打印2,否则打印不匹配"

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

至少是这个主意.

我可以看到用户输入的内容是否在字典中,并在标签上打印一些字符串,而不是在条目中键入的键值.

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.

我刚刚开始使用python进行编程和练习.我在这个问题上搜索了大约两天,只能找到基本上跳过if语句并直接转到else的循环.或者,如果该条目是"A",它将输出值3.我认为这是某种反向字典.所以我想自己弄清楚.如果我走对了路,那对我来说太好了,但是如果我完全错了,那么..

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..

所以我尝试了一个普通的if else语句,一个for循环,并为字典使用方法.

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)

我也尝试过

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

在该示例中,我获得了相应的值,但仅对于我输入的输入[0],[1]等有效,用于字典中的项目.

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.

推荐答案

由于它是字典,因此您可以直接使用 get()来获取键的值.

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")

这篇关于使用用户输入来搜索字典键并打印与值匹配的键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆