我正在尝试使用Python和Chemspider API创建化学摩尔计算器。我得到了这个代码,它按名称搜索数据库:
===这项工作很精细===
import tkinter as tk
from tkinter import ttk
import chemspipy
from chemspipy import ChemSpider
cs = ##MY CHEMSPIDER ID
def NameSearch(*args, **kwargs):
namesearch = tk.Tk()
namesearch.title("Search via Name")
def ChemSearch(*args, **kwargs):
try:
ChemName = ChemicalName.get()
ChemName = cs.search(ChemName)
if len(ChemName) >= 1:
ChemicalID.set(ChemName[0])
print(ChemicalID)
else:
popupmsg("Sorry, we could not find that chemical. Please try again")
except ValueError:
pass
searchframe = ttk.Frame(namesearch)
searchframe.grid(column=0, row=0, sticky="nsew")
searchframe.columnconfigure(0, weight=1)
searchframe.rowconfigure(0, weight=1)
ChemicalName = tk.StringVar()
ChemicalID = tk.StringVar()
Chemical_entry = ttk.Entry(searchframe, width=7, textvariable=ChemicalName)
Chemical_entry.grid(column=3, row=1, sticky="we")
ttk.Label(searchframe, textvariable=ChemicalID).grid(column=2, row=3, sticky=("ew"))
ttk.Button(searchframe, text="Search", command=ChemSearch).grid(column=3, row=4, sticky="w")
ttk.Label(searchframe, text="Chemical Name").grid(column=1, row=1, sticky="w")
ttk.Label(searchframe, text="The ID of this chemical is: ").grid(column=1, row=3, sticky="e")
for child in searchframe.winfo_children(): child.grid_configure(padx=5, pady=5)
Chemical_entry.focus()
namesearch.bind("<Return>", ChemSearch)
NameSearch()
然后我决定使用tkinter为我的程序实现一个菜单:
cs = ##MY CHEMSPIDER ID
def popupmsg(msg):
popup = tk.Tk()
def leavemini():
popup.destroy()
popup.wm_title("!")
label = ttk.Label(popup, text=msg)
label.pack(side="top", fill="x", pady=10)
B1 = ttk.Button(popup, text="Okay", command = leavemini)
B1.pack()
def NameSearch(cs, *args, **kwargs):
namesearch = tk.Tk()
namesearch.title("Search via Name")
def ChemSearch(*args, **kwargs):
try:
ChemName = ChemicalName.get()
ChemName = cs.search(ChemName)
if len(ChemName) >= 1:
ChemicalID.set(ChemName[0])
else:
popupmsg("Sorry, we could not find that chemical. Please try again")
print(ChemicalID)
except ValueError:
print("a")
pass
searchframe = ttk.Frame(namesearch)
searchframe.grid(column=0, row=0, sticky="nsew")
searchframe.columnconfigure(0, weight=1)
searchframe.rowconfigure(0, weight=1)
ChemicalName = tk.StringVar()
ChemicalID = tk.StringVar()
Chemical_entry = ttk.Entry(searchframe, width=7, textvariable=ChemicalName)
Chemical_entry.grid(column=3, row=1, sticky="we")
ttk.Label(searchframe, textvariable=ChemicalID).grid(column=2, row=3, sticky=("ew"))
ttk.Button(searchframe, text="Search", command=ChemSearch).grid(column=3, row=4, sticky="w")
ttk.Label(searchframe, text="Chemical Name").grid(column=1, row=1, sticky="w")
ttk.Label(searchframe, text="The ID of this chemical is: ").grid(column=1, row=3, sticky="e")
for child in searchframe.winfo_children(): child.grid_configure(padx=5, pady=5)
Chemical_entry.focus()
namesearch.bind("<Return>", ChemSearch)
class ChemApp(tk.Tk):
def __init__(self, *args, **kwargs):
tk.Tk.__init__(self, *args, **kwargs)
tk.Tk.wm_title(self, "Chemistry App")
container = tk.Frame(self)
container.pack(side="top", fill="both", expand = True)
container.grid_rowconfigure(0, weight=1)
container.grid_columnconfigure(0, weight=1)
menubar = tk.Menu(container)
searchmenu = tk.Menu(menubar, tearoff=0)
searchmenu.add_command(label="Search by Name", command=lambda: NameSearch(cs))
searchmenu.add_separator()
searchmenu.add_command(label="Exit", command=quit)
menubar.add_cascade(label="Search", menu=searchmenu)
tk.Tk.config(self, menu=menubar)
self.frames = {}
for F in (StartPage,):
frame = F(container, self)
self.frames[F] = frame
frame.grid(row=0, column=0, sticky="nsew")
self.show_frame(StartPage)
def show_frame(self, cont):
frame = self.frames[cont]
frame.tkraise()
class StartPage(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self,parent)
label = ttk.Label(self, text="Start Page")
label.pack(pady=10,padx=10)
button = ttk.Button(self, text="Search for a Chemical",
command=lambda: controller.show_frame(NameSearch))
button.pack()
app = ChemApp()
app.geometry("1280x720")
app.mainloop()
然而,当通过按下tkinter菜单按钮(在GUI中搜索下)触发的功能运行时,它拒绝从数据库返回正确的值。据我所知,我已经传递了所有必需的变量,并且它在以前的相同环境中运行,它只是不起作用。 任何帮助都会非常感激,因为它一直令人不安
答案 0 :(得分:0)
您不能在应用程序中拥有多个Tk
的实例。您需要拨打Tk()
一次。如果您需要更多窗口,请创建Toplevel
。