显示所有快译通在新窗口GUI使用标签的Tkinter的Python

显示所有快译通在新窗口GUI使用标签的Tkinter的Python

问题描述:

我是新来的蟒蛇会虽然很少在网上training.I不能得到任何靠近以下涉及的问题。只有显示所有快译通在新窗口GUI使用标签的Tkinter的Python

我使用Tkinter的GUI

from Tkinter import * 

root = Tk() 
trainings = {"title":"Python Training Course for Beginners", 
        "location":"Frankfurt", 
        "ID": 111,"title":"Intermediate Python Training", 
        "location":"Berlin", 
        "ID": 133,"title":"Python Text Processing Course", 
        "location":"Mdsgtd", 
        "ID": 122} 

    for key in trainings.keys(): 
    x = trainings.get(key) 
    print x 


    Label(root, text = x).pack() 
    mainloop() 

获取输出:122

但我期待的结果应该是在GUI的标签显示:

{'ID': 111, 'location': 'Frankfurt', 'title': 'Python Training Course for Beginners'} 
{'ID': 122, 'location': 'Mdsgtd', 'title': 'Python Text Processing Course'} 
{'ID': 133, 'location': 'Berlin', 'title': 'Intermediate Python Training'} 

我可以在函数内部使用标签,如下面的代码:这是不工作:

def OnButtonClick(self): 
    self.top= Toplevel() 
    self.top.title("Read Data Service Menu Item") 
    self.topdata = {'parakeet': ['fly', 'bird'], 'dog': 'animal', 'cat': 'feline'} 
    for key in self.topdata.keys(): 
       x = self.topdata.get(key) 

    self.topL2 = Label(self.top, text = key).pack() 

    self.top.resizable(1,0) 
    self.top.transient(self) 
    self.B1.config(state = 'normal') #disable/normal 

    self.topButton = Button(self.top, text = 'Close', command = self.OnChildClose) 
    self.topButton.pack() 
+1

你在字典中多次使用密钥 - 你不应该这样做! – asongtoruin

+0

压痕这是否'标签(根,文本= X).pack()'只调用与'x' –

您目前有几个问题,如在评论中指出的。首先,你应该改变你的trainings字典是字典的列表,让你存储依次在每个过程中的相关信息。

假设你要显示与每个过程中的信息不同的标签,下面应该工作:

from Tkinter import * 

courses = [{"title": "Python Training Course for Beginners", 
      "location": "Frankfurt", 
      "ID": 111}, 
      {"title": "Intermediate Python Training", 
      "location": "Berlin", 
      "ID": 133}, 
      {"title": "Python Text Processing Course", 
      "location": "Mdsgtd", 
      "ID": 122}] 


root = Tk() 

for course in courses: 
    temp_text = '{0} ({1}) - {2}'.format(course['title'], course['ID'], course['location']) 
    Label(root, text=temp_text).pack() 

mainloop() 

我们使用字符串格式化来创建一个很好的写输出,课程名称后面的它的ID在括号中,然后是破折号后的路线的位置。

这里重要的是我们要为每个课程创建一个Label小部件 - 因此,我们在for循环中添加新的Label以确保发生这种情况。

+0

的最后一个值如果我的ID是在HEX像0x111,0x122。然后我怎样才能在标签 –

+0

显示相同的十六进制@BharatKathari用'INT(当然[“ID”],0)'在行其中'temp_text'创建 – asongtoruin

+0

其工作取代'当然[“ID”]',现在,我希望在下面的标签中显示,而不必改变课程清单:1.初学者的Python培训课程法兰克福111,2.中级Python培训222 ....基本上添加sl。数字在开始 –