[英]Outputting a single cell from a csv file in python tkinter
This is meant to display a single element name from a CSV periodic table (atomic symbol in row 0, name in row 1) in a new label in a tkinter label. 这意味着在tkinter标签的新标签中显示CSV元素周期表中的单个元素名称(第0行中的原子符号,第1行中的名称)。 The user should be able to entry an atomic symbol, click the find button and the name be output.
用户应该能够输入原子符号,单击查找按钮,然后输出名称。 However so far, it does not print anything, and I am unsure which part is going wrong as I am fairly new to tkinter.
但是到目前为止,它什么也没打印,而且我不确定哪个部分出了问题,因为我对tkinter相当陌生。
The current code: 当前代码:
import tkinter as tk
base_container = tk.Tk()
element = tk.StringVar()
def search(element):
import csv
choice = element
with open('Periodic_Table.csv') as file:
csv_file = csv.reader(open('Periodic_Table.csv', "rt"), delimiter=",")
for row in csv_file:
if choice == str(row[0]):
tk.Label(base_container, text=str(row[1])).grid(row=4, column=0)
entry = tk.Label(base_container, text="Enter a chemical symbol:").grid(row=0, column=0, sticky="NE")
text = tk.Entry(base_container, width= 30, textvariable=element).grid(row=0, column=1, padx=2, pady=2, sticky="NE", columnspan=4)
Find = tk.Button(base_container, text="Find", command=lambda: search('element'))
Find.grid(row=30, column=10, sticky="SW", padx=2, pady=2)
Quit = tk.Button(base_container, text="Quit").grid(row=50, column=10, sticky="SW", padx=2, pady=2)
base_container.mainloop()
Periodic_Table.csv
starts as follows: Periodic_Table.csv
开始如下:
H, Hydrogen
He, Helium
Li, Lithium
Be, Beryllium
B, Boron
C, Carbon
There are a few problems with your code. 您的代码有一些问题。 As Martin mentioned, you're opening the CSV file twice.
如Martin所述,您要打开CSV文件两次。 And there's no need to open it & read every time you want to perform a search.
而且,您无需每次想进行搜索时都打开并阅读。 It's much more efficient to read it once into some kind of collection, and then perform your search on that collection.
一次将其读入某种集合,然后在该集合上执行搜索,效率会更高。
We could save the periodic table data into a list, but it's much better to use a dictionary. 我们可以将元素周期表数据保存到列表中,但是使用字典要好得多。 That way we can instantly get the element name by using the symbol as the key.
这样,我们可以通过使用符号作为键来立即获得元素名称。
Another problem that I hinted at earlier is that you're passing the string 'element'
to your search function instead of a string containing the element's symbol. 我之前暗示的另一个问题是,您正在将字符串
'element'
传递给搜索函数,而不是包含元素符号的字符串。 And since the periodic table doesn't contain the word 'element' the search would always fail. 而且由于元素周期表中不包含“元素”一词,搜索将始终失败。
We don't actually need to pass anything to search
: we can get it to retrieve the current contents of the Entry via the element
StringVar. 实际上,我们不需要传递任何内容来进行
search
:我们可以通过StringVar element
获取它来检索Entry的当前内容。
Also, as I mentioned above, it's not a good idea to make a new Label each time you want to update the output. 另外,如上所述,在每次要更新输出时都新建一个Label不是一个好主意。 The old Labels will still exist, but they'll be buried under the newer ones.
旧标签仍然存在,但是将被埋在新标签的下面。 Instead, create a single Label and update its text via its
.config
method. 而是创建一个Label并通过其
.config
方法更新其文本。
Here's a repaired version of your program. 这是您程序的修复版本。
import csv
import tkinter as tk
# Read the periodic_table data and save it to a dictionary, keyed by the symbol
periodic_table = {}
with open('Periodic_Table.csv', newline='') as f:
reader = csv.reader(f, delimiter=",", skipinitialspace=True)
for sym, name in reader:
periodic_table[sym] = name
base_container = tk.Tk()
element = tk.StringVar()
def search():
sym = element.get()
name = periodic_table.get(sym, 'Not found')
element_label.config(text=name)
tk.Label(base_container, text="Enter a chemical symbol:").grid(row=0, column=0, sticky="NE")
tk.Entry(base_container, width=30, textvariable=element).grid(row=0, column=1, padx=2, pady=2, sticky="NE", columnspan=4)
element_label = tk.Label(base_container, text='')
element_label.grid(row=4, column=0)
tk.Button(base_container, text="Find", command=search).grid(row=30, column=10, sticky="SW", padx=2, pady=2)
tk.Button(base_container, text="Quit", command=base_container.destroy).grid(row=50, column=10, sticky="SW", padx=2, pady=2)
base_container.mainloop()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.