我可以使用具有用户定义函数的字典吗?

时间:2016-03-23 19:14:56

标签: python-2.7 dictionary tkinter

我试图制作一个周期表(一个简单的表),在其中我可以输入一个元素的原子序数,它将提供该元素的详细信息。我已经创建了窗口和标签,但问题在于按钮。我试图通过按钮功能使用字典。谁能告诉我这里我做错了什么?谢谢

     from Tkinter import *

    """p={}
    p={"1":["hydrogen",'group 1','Electronic configuration- 1s1'] ,
       "2":["helium",'group 18','Electronic configuration- 1s2'],
       "3":["lithium",'group 1','Electronic configuration- [He] 2s1'],
       "4":["beryllium",'group 2','Electronic configuration- [He] 2s2'],
       "5":["boron",'group 13','Electronic configuration- [He] 2s2 2p1'],
       "6":["carbon",'group 14','Electronic configuration- [He] 2s2 2p2'],
       "7":["nitrogen",'group 15','Electronic configuration- [He] 2s2 2p3'],
       "8":["oxygen",'group 16','Electronic configuration- [He] 2s2 2p4'],
       "9":["fluorine",'group 17','Electronic configuration- [He] 2s2 2p5'],
       "10":["neon",'group 18','Electroni?c configuration- [He] 2s2 2p6'],
         }
       def search():
         x=raw_input('enter the atomic no. of the element you wanna search')
         for i in x:
            if i==x:
              print p[i]
            else:
               break
       search()


         def search():
            c=Label(ptable,text='Help me ',fg='blue',bg='red',font= ('Helpme',14)).place(relx=0.5,rely=0.8, anchor=S) 




    ptable=Tk()
    ptable.geometry('550x250+100+50')
    ptable.title('Periodic Table')
    ptable.config(background='red') # we can use either 'config' or   'configure', farak nin pdega


    f=StringVar()




    a=Label(ptable,text='My Periodic table', fg='blue',font=('My Periodic  table',16,'bold'),bg='red').place(relx=0.5, rely=0.05, anchor=CENTER)
    b=Label(ptable,text='Enter atomic number of the element',bg='red',font= ('Enter atomic number of the element',13,'bold')).place(relx=0,rely=0.3,  anchor=W)


    x=Entry(ptable,textvariable=f).place(relx=0.9, rely=0.3, anchor=E)



    button=Button(ptable,text='FIND OUT!',command=search).place(relx=0.5, rely= 0.6 , anchor=S)






    ptable.mainloop() 

虽然你可以告诉我如果我想在这个程序中添加图像怎么办?谢谢^^

1 个答案:

答案 0 :(得分:0)

以下内容并不总是有效。为大于9的数字添加打印语句,以便自己查看。也不要使用“i”,“l”或“o”作为单个字符名称,因为它们看起来像数字。

    for i in x:
        if i==x: