我有一个学校项目,我需要一些帮助。另外,很抱歉,我的课程不是全英文的,但它是为捷克儿童设计的,旨在帮助他们学习化学元素。我将尝试包含所有文件。该程序本身是第一部分。我是 StackOverflow 的新手,所以如果有更简单的方法来共享多个文件,我不知道。所以我对任何想要度过这个烂摊子的人感到抱歉。
程序创建元素周期表,点击一个元素后,会给出有关所选元素的信息。还有 2 个附加按钮。 1. 重置 - 删除加载的信息并显示通用消息。 2. 播放 - 生成一个随机元素,猜测了多少次,并检查玩家选择的元素是否匹配。
我在计算需要多少次猜测时遇到了问题。我认为这是因为按钮是在不同的功能中创建的。所以我试图查找如何从不同的函数访问变量,但我无法为我的程序提供任何解决方案。
检查系统也不起作用。我在想是否可以将它作为一个单独的功能来提供帮助。我明天会去做,但我很乐意提供任何帮助。
这是完整的程序。 5 个单独的文件。
import tkinter as tk
from random import randint
import json
class PeriodicTable(tk.Frame):
def __init__(self, parent):
tk.Frame.__init__(self, parent)
'''Initializes a new instance of the PeriodicTable class'''
self.default_title = "Periodická tabulka prvků"
self.default_header = "Klikněte na prvek pro zobrazení víc informací."
self.reset_button_text = "Reset"
self.play_button_text = "Play"
self.button_clicks = 0
# get data for building the elements table
self.elements_data = self.get_elements_data()
self.elements_map = self.get_elements_map()
self.elements_color_map = self.get_elements_color_map()
# add controls on UI
self.buttons = {}
self.parent = parent
self.winfo_toplevel().title(self.default_title)
self.topLabel = tk.Label(self, text=self.default_header, font=('Times', 40))
self.topLabel.grid(row=0, column=0, columnspan=18)
self.build_elements_table()
self.infoLine = tk.Label(self, text="", justify='left', font=('Helvetica bold',20))
self.infoLine.grid(row=1, column=3, columnspan=10, rowspan=4)
self.reset_button = tk.Button(self, text=self.reset_button_text, width=5, height=2, bg= "#124f3a", fg = "white",
command=lambda text="": (self.change_header(self.default_header), self.change_info("")))
self.reset_button.grid(row=13, column=0)
self.play_button = tk.Button(self, text=self.play_button_text, width=5, height=2, bg= "#2b239a", fg = "white",
command=lambda: self.game_body())
self.play_button.grid(row=13, column=18)
def get_elements_data(self):
with open('elements.json') as file:
return json.loads(file.read())
def get_elements_map(self):
with open('elements_layout_map.json') as file:
return json.loads(file.read())
def get_elements_color_map(self):
with open('color_map.json') as file:
return json.loads(file.read())
def build_elements_table(self):
'''Adds button controls for each element on UI'''
for element in self.elements_data["elements"]:
name = element["Short"]
row = self.elements_map["map"][name]["row"]
col = self.elements_map["map"][name]["column"]
color = self.elements_color_map['map'][element["Categories"][0]]
def get_info(element):
return "Protonove cislo = {}\nAtomova hmotnost = {}\nSkupenstvi = {}\nSkupina = {}".format(
element['Atomic Number'],
element['Atomic Weight'],
element['State'],
element['Categories'][0])
button = tk.Button(self, text=name, bg=color,
command=lambda e=element: (self.change_header(e["Name"]), self.change_info(get_info(e))))
button.config(width = 12, height= 4)
button.grid(row=row, column=col,sticky="NSEW")
self.buttons[name] = button
self.fillerLine = tk.Label(self, text="")
self.fillerLine.grid(row=10, column=0)
def change_header(self, text):
self.topLabel.config(text=text)
def change_info(self, text):
self.infoLine.config(text=text)
def game_body(self):
with open('game.json') as base:
data = json.load(base)
element = data["elements"]
choose_random = randint(0, len(element)-1)
chosen_el = element[choose_random]['Name']
short = element[0]["Short"]
self.guess = tk.Label(self, text=chosen_el, font=('Times',20))
self.guess.grid(row=0,column=2,columnspan=2)
self.topLabel.config(text="")
self.infoLine.config(text="")
self.create_widget()
selected = (0, len(element)-1)
if choose_random == selected:
self.correct = tk.Label(self, text="Correct", font=('Times',20))
self.correct.grid(row=12, column=5)
if not choose_random == selected:
self.not_correct = tk.Label(self, text="Try again", font=('Times',20))
self.not_correct.grid(row=14, column=5)
def create_widget(self):
self.count = tk.Label(self)
#self.count['command'] = self.update_count
self.count['text'] = "Celkem pokusů: " + str(self.button_clicks)
self.count.grid(row=14, column=5, columnspan=10, rowspan=4)
def update_count(self):
self.button_clicks += 1
root = tk.Tk()
app = PeriodicTable(root)
app.grid(row=0, column=0, sticky="NSEW")
root.state('zoomed')
root.minsize(width=1000, height=500)
app.place(relx=0.5, rely=0.5, anchor="center")
app.mainloop()
游戏.json
{
"version": 1,
"elements": [
{
"Short": "H",
"Name": "Vodik",
"Atomic Number": 1,
"Atomic Weight": 1.01,
"State": "Plyny",
"Categories": ["Nekovy"]
},
{
"Short": "Li",
"Name": "Lithium",
"Atomic Number": 3,
"Atomic Weight": 6.94,
"State": "Pevne",
"Categories": ["Alkalicke kovy"]
},
{
"Short": "Na",
"Name": "Sodik",
"Atomic Number": 11,
"Atomic Weight": 22.99,
"State": "Pevne",
"Categories": ["Alkalicke kovy"]
},
{
"Short": "K",
"Name": "Draslik",
"Atomic Number": 19,
"Atomic Weight": 39.10,
"State": "Pevne",
"Categories": ["Alkalicke kovy"]
},
{
"Short": "Rb",
"Name": "Rubidium",
"Atomic Number": 37,
"Atomic Weight": 85.47,
"State": "Pevne",
"Categories": ["Alkalicke kovy"]
},
{
"Short": "Cs",
"Name": "Cesium",
"Atomic Number": 55,
"Atomic Weight": 132.91,
"State": "Pevne",
"Categories": ["Alkalicke kovy"]
},
{
"Short": "Fr",
"Name": "Francium",
"Atomic Number": 87,
"Atomic Weight": 223.00,
"State": "Pevne",
"Categories": ["Alkalicke kovy"]
},
{
"Short": "Be",
"Name": "Beryllium",
"Atomic Number": 4,
"Atomic Weight": 9.01,
"State": "Pevne",
"Categories": ["Kovy alkalickych zemin"]
},
{
"Short": "Mg",
"Name": "Horcik",
"Atomic Number": 12,
"Atomic Weight": 24.31,
"State": "Pevne",
"Categories": ["Kovy alkalickych zemin"]
},
{
"Short": "Ca",
"Name": "Vapnik",
"Atomic Number": 20,
"Atomic Weight": 40.08,
"State": "Pevne",
"Categories": ["Kovy alkalickych zemin"]
},
{
"Short": "Sr",
"Name": "Stroncium",
"Atomic Number": 38,
"Atomic Weight": 87.62,
"State": "Pevne",
"Categories": ["Kovy alkalickych zemin"]
},
{
"Short": "Ba",
"Name": "Barium",
"Atomic Number": 56,
"Atomic Weight": 137.33,
"State": "Pevne",
"Categories": ["Kovy alkalickych zemin"]
},
{
"Short": "Ra",
"Name": "Radium",
"Atomic Number": 88,
"Atomic Weight": 226.03,
"State": "Pevne",
"Categories": ["Kovy alkalickych zemin"]
},
{
"Short": "Sc",
"Name": "Skandium",
"Atomic Number": 21,
"Atomic Weight": 44.96,
"State": "Pevne",
"Categories": ["Prechodne kovy"]
},
{
"Short": "Y",
"Name": "Yttrium",
"Atomic Number": 39,
"Atomic Weight": 88.91,
"State": "Pevne",
"Categories": ["Prechodne kovy"]
},
{
"Short": "La >|",
"Name": "Lanthan",
"Atomic Number": 57,
"Atomic Weight": 138.91,
"State": "Pevne",
"Categories": ["Prechodne kovy"]
},
{
"Short": "Ac >|",
"Name": "Actinium",
"Atomic Number": 89,
"Atomic Weight": 227.03,
"State": "Pevne",
"Categories": ["Prechodne kovy"]
},
{
"Short": "Ti",
"Name": "Titan",
"Atomic Number": 22,
"Atomic Weight": 47.90,
"State": "Pevne",
"Categories": ["Prechodne kovy"]
},
{
"Short": "Zr",
"Name": "Zirconium",
"Atomic Number": 40,
"Atomic Weight": 91.22,
"State": "Pevne",
"Categories": ["Prechodne kovy"]
},
{
"Short": "Hf",
"Name": "Hafnium",
"Atomic Number": 72,
"Atomic Weight": 178.49,
"State": "Pevne",
"Categories": ["Prechodne kovy"]
},
{
"Short": "Rf",
"Name": "Rutherfordium",
"Atomic Number": 104,
"Atomic Weight": 261.00,
"State": "Synteticke",
"Categories": ["Prechodne kovy"]
},
{
"Short": "V",
"Name": "Vanad",
"Atomic Number": 23,
"Atomic Weight": 50.94,
"State": "Pevne",
"Categories": ["Prechodne kovy"]
},
{
"Short": "Nb",
"Name": "Niobium",
"Atomic Number": 41,
"Atomic Weight": 92.91,
"State": "Pevne",
"Categories": ["Prechodne kovy"]
},
{
"Short": "Ta",
"Name": "Tantal",
"Atomic Number": 73,
"Atomic Weight": 180.95,
"State": "Pevne",
"Categories": ["Prechodne kovy"]
},
{
"Short": "Db",
"Name": "Dubnium",
"Atomic Number": 105,
"Atomic Weight": 262.00,
"State": "Synteticke",
"Categories": ["Prechodne kovy"]
},
{
"Short": "Cr",
"Name": "Chrom",
"Atomic Number": 24,
"Atomic Weight": 51.99,
"State": "Pevne",
"Categories": ["Prechodne kovy"]
},
{
"Short": "Mo",
"Name": "Molybden",
"Atomic Number": 42,
"Atomic Weight": 95.94,
"State": "Pevne",
"Categories": ["Prechodne kovy"]
},
{
"Short": "W",
"Name": "Wolfram",
"Atomic Number": 74,
"Atomic Weight": 183.85,
"State": "Pevne",
"Categories": ["Prechodne kovy"]
},
{
"Short": "Sg",
"Name": "Seaborgium",
"Atomic Number": 106,
"Atomic Weight": 266.00,
"State": "Synteticke",
"Categories": ["Prechodne kovy"]
},
{
"Short": "Mn",
"Name": "Mangan",
"Atomic Number": 25,
"Atomic Weight": 178.49,
"State": "Pevne",
"Categories": ["Prechodne kovy"]
},
{
"Short": "Tc",
"Name": "Technecium",
"Atomic Number": 43,
"Atomic Weight": 178.49,
"State": "Synteticke",
"Categories": ["Prechodne kovy"]
},
{
"Short": "Re",
"Name": "Rhenium",
"Atomic Number": 75,
"Atomic Weight": 178.49,
"State": "Pevne",
"Categories": ["Prechodne kovy"]
},
{
"Short": "Bh",
"Name": "Bohrium",
"Atomic Number": 107,
"Atomic Weight": 262.00,
"State": "Synteticke",
"Categories": ["Prechodne kovy"]
},
{
"Short": "Fe",
"Name": "Zelezo",
"Atomic Number": 26,
"Atomic Weight": 55.85,
"State": "Pevne",
"Categories": ["Prechodne kovy"]
},
{
"Short": "Ru",
"Name": "Ruthenium",
"Atomic Number": 44,
"Atomic Weight": 101.07,
"State": "Pevne",
"Categories": ["Prechodne kovy"]
},
{
"Short": "Os",
"Name": "Osmium",
"Atomic Number": 76,
"Atomic Weight": 190.20,
"State": "Pevne",
"Categories": ["Prechodne kovy"]
},
{
"Short": "Hs",
"Name": "Hassium",
"Atomic Number": 108,
"Atomic Weight": 265.00,
"State": "Synteticke",
"Categories": ["Prechodne kovy"]
},
{
"Short": "Co",
"Name": "Kobalt",
"Atomic Number": 27,
"Atomic Weight": 58.93,
"State": "Pevne",
"Categories": ["Prechodne kovy"]
},
{
"Short": "Rh",
"Name": "Rhodium",
"Atomic Number": 45,
"Atomic Weight": 102.91,
"State": "Pevne",
"Categories": ["Prechodne kovy"]
},
{
"Short": "Ir",
"Name": "Iridium",
"Atomic Number": 77,
"Atomic Weight": 192.22,
"State": "Pevne",
"Categories": ["Prechodne kovy"]
},
{
"Short": "Mt",
"Name": "Meitnerium",
"Atomic Number": 109,
"Atomic Weight": 266.00,
"State": "Synteticke",
"Categories": ["Prechodne kovy"]
},
{
"Short": "Ni",
"Name": "Nikl",
"Atomic Number": 28,
"Atomic Weight": 58.70,
"State": "Pevne",
"Categories": ["Prechodne kovy"]
},
{
"Short": "Pd",
"Name": "Palladium",
"Atomic Number": 46,
"Atomic Weight": 106.40,
"State": "Pevne",
"Categories": ["Prechodne kovy"]
},
{
"Short": "Pt",
"Name": "Platina",
"Atomic Number": 78,
"Atomic Weight": 195.09,
"State": "Pevne",
"Categories": ["Prechodne kovy"]
},
{
"Short": "Cu",
"Name": "Med",
"Atomic Number": 29,
"Atomic Weight": 63.55,
"State": "Pevne",
"Categories": ["Prechodne kovy"]
},
{
"Short": "Ag",
"Name": "Stribro",
"Atomic Number": 47,
"Atomic Weight": 107.97,
"State": "Pevne",
"Categories": ["Prechodne kovy"]
},
{
"Short": "Au",
"Name": "Zlato",
"Atomic Number": 79,
"Atomic Weight": 196.97,
"State": "Pevne",
"Categories": ["Prechodne kovy"]
},
{
"Short": "Zn",
"Name": "Zinek",
"Atomic Number": 30,
"Atomic Weight": 65.37,
"State": "Pevne",
"Categories": ["Prechodne kovy"]
},
{
"Short": "Cd",
"Name": "Kadmium",
"Atomic Number": 48,
"Atomic Weight": 112.41,
"State": "Pevne",
"Categories": ["Prechodne kovy"]
},
{
"Short": "Hg",
"Name": "Rtut",
"Atomic Number": 80,
"Atomic Weight": 200.59,
"State": "Kapalne",
"Categories": ["Prechodne kovy"]
},
{
"Short": "B",
"Name": "Bor",
"Atomic Number": 5,
"Atomic Weight": 10.81,
"State": "Pevne",
"Categories": ["Polokovy"]
},
{
"Short": "Al",
"Name": "Hlinik",
"Atomic Number": 13,
"Atomic Weight": 26.98,
"State": "Pevne",
"Categories": ["Kovy"]
},
{
"Short": "Ga",
"Name": "Gallium",
"Atomic Number": 31,
"Atomic Weight": 69.72,
"State": "Pevne",
"Categories": ["Kovy"]
},
{
"Short": "In",
"Name": "Indium",
"Atomic Number": 49,
"Atomic Weight": 69.72,
"State": "Pevne",
"Categories": ["Kovy"]
},
{
"Short": "Tl",
"Name": "Thallium",
"Atomic Number": 81,
"Atomic Weight": 204.37,
"State": "Pevne",
"Categories": ["Kovy"]
},
{
"Short": "C",
"Name": "Uhlik",
"Atomic Number": 6,
"Atomic Weight": 12.01,
"State": "Pevne",
"Categories": ["Nekovy"]
},
{
"Short": "Si",
"Name": "Kremik",
"Atomic Number": 14,
"Atomic Weight": 28.09,
"State": "Pevne",
"Categories": ["Polokovy"]
},
{
"Short": "Ge",
"Name": "Germanium",
"Atomic Number": 32,
"Atomic Weight": 72.59,
"State": "Pevne",
"Categories": ["Polokovy"]
},
{
"Short": "Sn",
"Name": "Cin",
"Atomic Number": 50,
"Atomic Weight": 118.69,
"State": "Pevne",
"Categories": ["Kovy"]
},
{
"Short": "Pb",
"Name": "Olovo",
"Atomic Number": 82,
"Atomic Weight": 207.20,
"State": "Pevne",
"Categories": ["Kovy"]
},
{
"Short": "N",
"Name": "Dusik",
"Atomic Number": 7,
"Atomic Weight": 14.01,
"State": "Plyny",
"Categories": ["Nekovy"]
},
{
"Short": "P",
"Name": "Fosfor",
"Atomic Number": 15,
"Atomic Weight": 30.97,
"State": "Pevne",
"Categories": ["Nekovy"]
},
{
"Short": "As",
"Name": "Arsen",
"Atomic Number": 33,
"Atomic Weight": 74.92,
"State": "Pevne",
"Categories": ["Polokovy"]
}
]
}
元素.json
(基本上和 game.json 一样,但我放不下)
elements_layout_map.json
{
"version": 1,
"map": {
"H": { "row": 1, "column" : 0 },
"Li": { "row": 2, "column" : 0 },
"Na": { "row": 3, "column": 0 },
"K": { "row": 4, "column": 0 },
"Rb": { "row": 5, "column": 0 },
"Cs": { "row": 6, "column": 0 },
"Fr": { "row": 7, "column": 0 },
"Be": { "row": 2, "column": 1 },
"Mg": { "row": 3, "column": 1 },
"Ca": { "row": 4, "column": 1 },
"Sr": { "row": 5, "column": 1 },
"Ba": { "row": 6, "column": 1 },
"Ra": { "row": 7, "column": 1 },
"Sc": { "row": 4, "column": 2 },
"Y": { "row": 5, "column": 2 },
"La >|": { "row": 6, "column": 2 },
"Ac >|": { "row": 7, "column": 2 },
"Ti": { "row": 4, "column": 3 },
"Zr": { "row": 5, "column": 3 },
"Hf": { "row": 6, "column": 3 },
"Rf": { "row": 7, "column": 3 },
"V": { "row": 4, "column": 4 },
"Nb": { "row": 5, "column": 4 },
"Ta": { "row": 6, "column": 4 },
"Db": { "row": 7, "column": 4 },
"Cr": { "row": 4, "column": 5 },
"Mo": { "row": 5, "column": 5 },
"W": { "row": 6, "column": 5 },
"Sg": { "row": 7, "column": 5 },
"Mn": { "row": 4, "column": 6 },
"Tc": { "row": 5, "column": 6 },
"Re": { "row": 6, "column": 6 },
"Bh": { "row": 7, "column": 6 },
"Fe": { "row": 4, "column": 7 },
"Ru": { "row": 5, "column": 7 },
"Os": { "row": 6, "column": 7 },
"Hs": { "row": 7, "column": 7 },
"Co": { "row": 4, "column": 8 },
"Rh": { "row": 5, "column": 8 },
"Ir": { "row": 6, "column": 8 },
"Mt": { "row": 7, "column": 8 },
"Ni": { "row": 4, "column": 9 },
"Pd": { "row": 5, "column": 9 },
"Pt": { "row": 6, "column": 9 },
"Cu": { "row": 4, "column": 10 },
"Ag": { "row": 5, "column": 10 },
"Au": { "row": 6, "column": 10 },
"Zn": { "row": 4, "column": 11 },
"Cd": { "row": 5, "column": 11 },
"Hg": { "row": 6, "column": 11 },
"B": { "row": 2, "column": 12 },
"Al": { "row": 3, "column": 12 },
"Ga": { "row": 4, "column": 12 },
"In": { "row": 5, "column": 12 },
"Tl": { "row": 6, "column": 12 },
"C": { "row": 2, "column": 13 },
"Si": { "row": 3, "column": 13 },
"Ge": { "row": 4, "column": 13 },
"Sn": { "row": 5, "column": 13 },
"Pb": { "row": 6, "column": 13 },
"N": { "row": 2, "column": 14 },
"P": { "row": 3, "column": 14 },
"As": { "row": 4, "column": 14 },
"Sb": { "row": 5, "column": 14 },
"Bi": { "row": 6, "column": 14 },
"O": { "row": 2, "column": 15 },
"S": { "row": 3, "column": 15 },
"Se": { "row": 4, "column": 15 },
"Te": { "row": 5, "column": 15 },
"Po": { "row": 6, "column": 15 },
"F": { "row": 2, "column": 16 },
"Cl": { "row": 3, "column": 16 },
"Br": { "row": 4, "column": 16 },
"I": { "row": 5, "column": 16 },
"At": { "row": 6, "column": 16 },
"He": { "row": 1, "column": 17 },
"Ne": { "row": 2, "column": 17 },
"Ar": { "row": 3, "column": 17 },
"Kr": { "row": 4, "column": 17 },
"Xe": { "row": 5, "column": 17 },
"Rn": { "row": 6, "column": 17 },
">| Ce": { "row": 11, "column": 3 },
"Pr": { "row": 11, "column": 4 },
"Nd": { "row": 11, "column": 5 },
"Pm": { "row": 11, "column": 6 },
"Sm": { "row": 11, "column": 7 },
"Eu": { "row": 11, "column": 8 },
"Gd": { "row": 11, "column": 9 },
"Tb": { "row": 11, "column": 10 },
"Dy": { "row": 11, "column": 11 },
"Ho": { "row": 11, "column": 12 },
"Er": { "row": 11, "column": 13 },
"Tm": { "row": 11, "column": 14 },
"Yb": { "row": 11, "column": 15 },
"Lu": { "row": 11, "column": 16 },
">| Th": { "row": 12, "column": 3 },
"Pa": { "row": 12, "column": 4 },
"U" : { "row": 12, "column": 5 },
"Np": { "row": 12, "column": 6 },
"Pu": { "row": 12, "column": 7 },
"Am": { "row": 12, "column": 8 },
"Cm": { "row": 12, "column": 9 },
"Bk": { "row": 12, "column": 10 },
"Cf": { "row": 12, "column": 11 },
"Es": { "row": 12, "column": 12 },
"Fm": { "row": 12, "column": 13 },
"Md": { "row": 12, "column": 14 },
"No": { "row": 12, "column": 15 },
"Lr": { "row": 12, "column": 16 }
}
}
color_map.json
{
"version": 1,
"map": {
"Alkalicke kovy": "#3895bc",
"Kovy alkalickych zemin": "#70c2b4",
"Prechodne kovy": "#b3dbbf",
"Nekovy": "Light Pink",
"Kovy": "#74c1cd",
"Polokovy": "#a1dae3",
"Halogeny": "#dbb59e",
"Vzacne plyny": "#986fb3"
}
}