from Tkinter import *
from copy import *
def relist(country):
if country == "India":
countries["India"] += 1
reclabel_1["text"] = "India: " + str(countries["India"])
if country == "USA":
countries["USA"] += 1
reclabel_2["text"] = "USA: " + str(countries["USA"])
if country == "UK":
countries["UK"] += 1
reclabel_3["text"] = "UK: " + str(countries["UK"])
if country == "China":
countries["China"] += 1
reclabel_4["text"] = "China: " + str(countries["China"])
if country == "Russian Federation":
countries["Russian Federation"] += 1
reclabel_5["text"] = "Russian Federation: " + str(countries["Russian Federation"])
if country == "France":
countries["France"] += 1
reclabel_6["text"] = "France: " + str(countries["France"])
if country == "Germany":
countries["Germany"] += 1
reclabel_7["text"] = "Germany: " + str(countries["Germany"])
if country == "Egypt":
countries["Egypt"] += 1
reclabel_8["text"] = "Egypt: " + str(countries["Egypt"])
if country == "Libya":
countries["Libya"] += 1
reclabel_9["text"] = "Libya: " + str(countries["Libya"])
if country == "Pakistan":
countries["Pakistan"] += 1
reclabel_10["text"] = "Pakistan: " + str(countries["Pakistan"])
if country == "DPRK":
countries["DPRK"] += 1
reclabel_11["text"] = "DPRK: " + str(countries["DPRK"])
if country == "ROK":
countries["ROK"] += 1
reclabel_12["text"] = "ROK: " + str(countries["ROK"])
if country == "Brazil":
countries["Brazil"] += 1
reclabel_13["text"] = "Brazil: " + str(countries["Brazil"])
if country == "Canada":
countries["Canada"] += 1
reclabel_14["text"] = "Canada: " + str(countries["Canada"])
if country == "Chile":
countries["Chile"] += 1
reclabel_15["text"] = "Chile: " + str(countries["Chile"])
if country == "Mexico":
countries["Mexico"] += 1
reclabel_16["text"] = "Mexico: " + str(countries["Mexico"])
if country == "Australia":
countries["Australia"] += 1
reclabel_17["text"] = "Australia: " + str(countries["Australia"])
if country == "Argentia":
countries["Argentia"] += 1
reclabel_18["text"] = "Argentia: " + str(countries["Argentia"])
if country == "Colombia":
countries["Colombia"] += 1
reclabel_19["text"] = "Colombia: " + str(countries["Colombia"])
if country == "Sri Lanka":
countries["Sri Lanka"] += 1
reclabel_20["text"] = "Sri Lanka: " + str(countries["Sri Lanka"])
global ordlist
sortedlist = sorted(countries.iteritems(), key=lambda t:t[1])
#print sortedlist
ordlist = [sortedlist[0][0],sortedlist[1][0],sortedlist[2][0],sortedlist[3][0],sortedlist[4][0],sortedlist[5][0],sortedlist[6][0],sortedlist[7][0],sortedlist[8][0],sortedlist[9][0],sortedlist[10][0],sortedlist[11][0],sortedlist[12][0],sortedlist[13][0],sortedlist[14][0],sortedlist[15][0],sortedlist[16][0],sortedlist[17][0],sortedlist[18][0],sortedlist[19][0]]
#print ordlist
button_1["text"] = ordlist[0]
button_2["text"] = ordlist[1]
button_3["text"] = ordlist[2]
button_4["text"] = ordlist[3]
button_5["text"] = ordlist[4]
button_6["text"] = ordlist[5]
button_7["text"] = ordlist[6]
button_8["text"] = ordlist[7]
button_9["text"] = ordlist[8]
button_10["text"] = ordlist[9]
button_11["text"] = ordlist[10]
button_12["text"] = ordlist[11]
button_13["text"] = ordlist[12]
button_14["text"] = ordlist[13]
button_15["text"] = ordlist[14]
button_16["text"] = ordlist[15]
button_17["text"] = ordlist[16]
button_18["text"] = ordlist[17]
button_19["text"] = ordlist[18]
button_20["text"] = ordlist[19]
countries = {"India":0,"USA":0,"UK":0,"China":0,"Russian Federation":0,"France":0,"Germany":0,"Egypt":0,"Libya":0,"Pakistan":0,"DPRK":0,"ROK":0,"Brazil":0,"Canada":0,"Chile":0,"Mexico":0,"Australia":0,"Argentia":0,"Colombia":0,"Sri Lanka":0}
countries2 = {}
ordlist = ["India","USA","UK","China","Russian Federation","France","Germany","Egypt","Libya","Pakistan","DPRK","ROK","Brazil","Canada","Chile","Mexico","Australia","Argentia","Colombia","Sri Lanka"]
currentclick = 1
root = Tk()
frame_buttons = Frame(root)
frame_buttons.pack(side = "left")
frame_current = Frame(root)
frame_current.pack()
frame_recognition = Frame(root)
frame_recognition.pack(side = "right")
#
reclabel_1 = Label(frame_recognition, text = "India: " + str(countries["India"]))
reclabel_2 = Label(frame_recognition, text = "USA: " + str(countries["USA"]))
reclabel_3 = Label(frame_recognition, text = "UK: " + str(countries["UK"]))
reclabel_4 = Label(frame_recognition, text = "China: " + str(countries["China"]))
reclabel_5 = Label(frame_recognition, text = "Russian Federation: " + str(countries["Russian Federation"]))
reclabel_6 = Label(frame_recognition, text = "France: " + str(countries["France"]))
reclabel_7 = Label(frame_recognition, text = "Germany: " + str(countries["Germany"]))
reclabel_8 = Label(frame_recognition, text = "Egypt: " + str(countries["Egypt"]))
reclabel_9 = Label(frame_recognition, text = "Libya: " + str(countries["Libya"]))
reclabel_10 = Label(frame_recognition, text = "Pakistan: " + str(countries["Pakistan"]))
reclabel_11 = Label(frame_recognition, text = "DPRK: " + str(countries["DPRK"]))
reclabel_12 = Label(frame_recognition, text = "ROK: " + str(countries["ROK"]))
reclabel_13 = Label(frame_recognition, text = "Brazil: " + str(countries["Brazil"]))
reclabel_14 = Label(frame_recognition, text = "Canada: " + str(countries["Canada"]))
reclabel_15 = Label(frame_recognition, text = "Chile: " + str(countries["Chile"]))
reclabel_16 = Label(frame_recognition, text = "Mexico: " + str(countries["Mexico"]))
reclabel_17 = Label(frame_recognition, text = "Australia: " + str(countries["Australia"]))
reclabel_18 = Label(frame_recognition, text = "Argentia: " + str(countries["Argentia"]))
reclabel_19 = Label(frame_recognition, text = "Colombia: " + str(countries["Colombia"]))
reclabel_20 = Label(frame_recognition, text = "Sri Lanka: " + str(countries["Sri Lanka"]))
reclabel_1.grid(row = 1, column = 1)
reclabel_2.grid(row = 2, column = 1)
reclabel_3.grid(row = 3, column = 1)
reclabel_4.grid(row = 4, column = 1)
reclabel_5.grid(row = 5, column = 1)
reclabel_6.grid(row = 6, column = 1)
reclabel_7.grid(row = 7, column = 1)
reclabel_8.grid(row = 8, column = 1)
reclabel_9.grid(row = 9, column = 1)
reclabel_10.grid(row = 10, column = 1)
reclabel_11.grid(row = 1, column = 2)
reclabel_12.grid(row = 2, column = 2)
reclabel_13.grid(row = 3, column = 2)
reclabel_14.grid(row = 4, column = 2)
reclabel_15.grid(row = 5, column = 2)
reclabel_16.grid(row = 6, column = 2)
reclabel_17.grid(row = 7, column = 2)
reclabel_18.grid(row = 8, column = 2)
reclabel_19.grid(row = 9, column = 2)
reclabel_20.grid(row = 10, column = 2)
################
label_1 = Label(frame_buttons,text ="1.")
label_2 = Label(frame_buttons,text ="2.")
label_3 = Label(frame_buttons,text ="3.")
label_4 = Label(frame_buttons,text ="4.")
label_5 = Label(frame_buttons,text ="5.")
label_6 = Label(frame_buttons,text ="6.")
label_7 = Label(frame_buttons,text ="7.")
label_8 = Label(frame_buttons,text ="8.")
label_9 = Label(frame_buttons,text ="9.")
label_10 = Label(frame_buttons,text ="10.")
label_1.grid(row = 1, column = 1)
label_2.grid(row = 2, column = 1)
label_3.grid(row = 3, column = 1)
label_4.grid(row = 4, column = 1)
label_5.grid(row = 5, column = 1)
label_6.grid(row = 6, column = 1)
label_7.grid(row = 7, column = 1)
label_8.grid(row = 8, column = 1)
label_9.grid(row = 9, column = 1)
label_10.grid(row = 10, column = 1)
button_1 = Button(frame_buttons,width = 20 , text = ordlist[0], command = lambda: relist(ordlist[0]))
button_2 = Button(frame_buttons,width = 20 , text = ordlist[1], command = lambda: relist(ordlist[1]))
button_3 = Button(frame_buttons,width = 20 , text = ordlist[2], command = lambda: relist(ordlist[2]))
button_4 = Button(frame_buttons,width = 20 , text = ordlist[3], command = lambda: relist(ordlist[3]))
button_5 = Button(frame_buttons,width = 20 , text = ordlist[4], command = lambda: relist(ordlist[4]))
button_6 = Button(frame_buttons,width = 20 , text = ordlist[5], command = lambda: relist(ordlist[5]))
button_7 = Button(frame_buttons,width = 20 , text = ordlist[6], command = lambda: relist(ordlist[6]))
button_8 = Button(frame_buttons,width = 20 , text = ordlist[7], command = lambda: relist(ordlist[7]))
button_9 = Button(frame_buttons,width = 20 , text = ordlist[8], command = lambda: relist(ordlist[8]))
button_10 = Button(frame_buttons,width = 20 , text = ordlist[9], command = lambda: relist(ordlist[9]))
button_1.grid(row = 1, column = 2)
button_2.grid(row = 2, column = 2)
button_3.grid(row = 3, column = 2)
button_4.grid(row = 4, column = 2)
button_5.grid(row = 5, column = 2)
button_6.grid(row = 6, column = 2)
button_7.grid(row = 7, column = 2)
button_8.grid(row = 8, column = 2)
button_9.grid(row = 9, column = 2)
button_10.grid(row = 10, column = 2)
label_11 = Label(frame_buttons,text ="11.")
label_12 = Label(frame_buttons,text ="12.")
label_13 = Label(frame_buttons,text ="13.")
label_14 = Label(frame_buttons,text ="14.")
label_15 = Label(frame_buttons,text ="15.")
label_16 = Label(frame_buttons,text ="16.")
label_17 = Label(frame_buttons,text ="17.")
label_18 = Label(frame_buttons,text ="18.")
label_19 = Label(frame_buttons,text ="19.")
label_20 = Label(frame_buttons,text ="20.")
label_11.grid(row = 1, column = 3)
label_12.grid(row = 2, column = 3)
label_13.grid(row = 3, column = 3)
label_14.grid(row = 4, column = 3)
label_15.grid(row = 5, column = 3)
label_16.grid(row = 6, column = 3)
label_17.grid(row = 7, column = 3)
label_18.grid(row = 8, column = 3)
label_19.grid(row = 9, column = 3)
label_20.grid(row = 10, column = 3)
button_11 = Button(frame_buttons,width = 20 , text = ordlist[10], command = lambda: relist(ordlist[10]))
button_12 = Button(frame_buttons,width = 20 , text = ordlist[11], command = lambda: relist(ordlist[11]))
button_13 = Button(frame_buttons,width = 20 , text = ordlist[12], command = lambda: relist(ordlist[12]))
button_14 = Button(frame_buttons,width = 20 , text = ordlist[13], command = lambda: relist(ordlist[13]))
button_15 = Button(frame_buttons,width = 20 , text = ordlist[14], command = lambda: relist(ordlist[14]))
button_16 = Button(frame_buttons,width = 20 , text = ordlist[15], command = lambda: relist(ordlist[15]))
button_17 = Button(frame_buttons,width = 20 , text = ordlist[16], command = lambda: relist(ordlist[16]))
button_18 = Button(frame_buttons,width = 20 , text = ordlist[17], command = lambda: relist(ordlist[17]))
button_19 = Button(frame_buttons,width = 20 , text = ordlist[18], command = lambda: relist(ordlist[18]))
button_20 = Button(frame_buttons,width = 20 , text = ordlist[19], command = lambda: relist(ordlist[19]))
button_11.grid(row = 1, column = 4)
button_12.grid(row = 2, column = 4)
button_13.grid(row = 3, column = 4)
button_14.grid(row = 4, column = 4)
button_15.grid(row = 5, column = 4)
button_16.grid(row = 6, column = 4)
button_17.grid(row = 7, column = 4)
button_18.grid(row = 8, column = 4)
button_19.grid(row = 9, column = 4)
button_20.grid(row = 10, column = 4)
root.mainloop()
我正在为全世界的学生制作模拟联合国课程。其中主席必须承认发言者。为了表彰委员会所有成员,我正在制作一个识别软件。问题在于,代码大多表现得像一个堆栈,第20个国家暂时停留在第20个国家。有人有任何想法吗?
答案 0 :(得分:0)
您可以替换所有(和添加国家/地区意味着仅更改列表)
reclabel_1 thru 20 = Label(frame_recognition, text = "India: " + str(countries["India"]))
##
## with (not tested)
label_dict={}
this_row=1
this_column=1
for country in ordlist:
reclabel = Label(frame_recognition, text = country + ": " + str(countries[country]))
reclabel.grid(row = this_row, column = this_column)
label_dict[country]=reclabel
this_column += 1
if this_column > 10:
this_column = 1
this_row += 1
##---------- likewise all of the
if country == "India":
countries["India"] += 1
reclabel_1["text"] = "India: " + str(countries["India"])
##
## with
countries[country] += 1 ## assumes country is in dictionary
label_dict[country]["text"] = country + ": " +str(countries[country])
## same as label_dict[country].config(text=) etc.
##---------- and the same for the Buttons and other Labels