首页 > 解决方案 > sqlalchemy.exc.NoResultFound:需要一个时没有找到行,Tkinter 回调中的异常

问题描述

我制作了以下代码。它是执行一些基本化学操作的基本乌龟。我不断收到此错误。

Exception in Tkinter callback
Traceback (most recent call last):
  File "C:\Users\Arnav\AppData\Local\Programs\Python\Python39\lib\tkinter\__init__.py", line 1892, in __call__
    return self.func(*args)
  File "C:\Users\Arnav\AppData\Local\Programs\Python\Python39\lib\turtle.py", line 674, in eventfun
    fun(x, y)
  File "c:\Users\Arnav\Desktop\VS Code\Python\periodicTable.py", line 41, in getCoords
    return optionClick(x,y)
  File "c:\Users\Arnav\Desktop\VS Code\Python\periodicTable.py", line 152, in optionClick
    atomicNum, name = getAtomicNumber('si')
  File "c:\Users\Arnav\Desktop\VS Code\Python\periodicTable.py", line 9, in getAtomicNumber
    userElement = mendeleev.element(element)
  File "C:\Users\Arnav\AppData\Local\Programs\Python\Python39\lib\site-packages\mendeleev\mendeleev.py", line 64, in element
    return _get_element(ids)
  File "C:\Users\Arnav\AppData\Local\Programs\Python\Python39\lib\site-packages\mendeleev\mendeleev.py", line 81, in _get_element
    return session.query(Element).filter(Element.symbol == str(ids)).one()
  File "C:\Users\Arnav\AppData\Local\Programs\Python\Python39\lib\site-packages\sqlalchemy\orm\query.py", line 2809, in one
    return self._iter().one()
  File "C:\Users\Arnav\AppData\Local\Programs\Python\Python39\lib\site-packages\sqlalchemy\engine\result.py", line 1374, in one
    return self._only_one_row(
  File "C:\Users\Arnav\AppData\Local\Programs\Python\Python39\lib\site-packages\sqlalchemy\engine\result.py", line 561, in _only_one_row       
    raise exc.NoResultFound(
sqlalchemy.exc.NoResultFound: No row was found when one was required

这是我的代码:

import turtle
from mendeleev import *
from turtle import *
import time



def getAtomicNumber(element):   
    userElement = mendeleev.element(element)    
    return userElement.atomic_number, userElement.name


def getElementName(element):
    userElement = mendeleev.element(element)
    
    return userElement.name
def getElementDiscoverer(element):
    userElement = mendeleev.element(element)
    return userElement.name, userElement.discoverers
def getElementElectrons(element):
    userElement = mendeleev.element(element)

    return userElement.electrons, userElement.name
def getIonizationEnergy(element):
    userElement = mendeleev.element(element)

    return userElement._ionization_energies,userElement.name
def getMassNumber(element):
    userElement = mendeleev.element(element)

    return userElement.mass_number, userElement.name
def getNameOrigin(element):
    userElement = mendeleev.element(element)

    return userElement.name_origin
def getEConfiguration(element):
    userElement = mendeleev.element(element)

    return userElement.econf, userElement.name
def getCoords(x,y):
    return optionClick(x,y)
def optionClick(x,y):
   
    
    print(x,y)
    writer = turtle.Turtle()
    writer.speed(0)
    writer.color('white')
    writer.penup()
    writer.hideturtle()
    writer.goto(0,-200)




    if -310<x<310 and 130>y>90:
        atomicNum, name = getAtomicNumber('si')
        writer.write(f'{name} has an atomic number of {atomicNum}')
        
    elif -310<x<310 and 90>=y>75:
        getElementName('si')
    elif -310<x<310 and 75>y>50:
        name, discoverer = getElementDiscoverer('si')


window = turtle.Screen()
window.setup(width=800, height=600)
window.title('Chemistry Tool')
window.bgcolor('black')
window.tracer(0)

pen = turtle.Turtle()
pen.speed(0)
pen.color('red')
pen.penup()
pen.hideturtle()
pen.goto(0,200)
pen.write("Welcome to the Chemistry Tool",align="center",font=('Courier New',26,'bold'))


choose = turtle.Turtle()
choose.speed(0)
choose.color('green')
choose.penup()
choose.hideturtle()
choose.goto(0,150)
choose.write("Please choose a function from the following:",align="center",font=('Courier New',16,'normal'))

choice_1 = turtle.Turtle()
choice_1.speed(0)
choice_1.color('blue')
choice_1.penup()
choice_1.hideturtle()
choice_1.goto(0,100)
choice_1.write("1. Get Atomic Number",align="center",font=('Courier New',15,'normal'))


choice_2 = turtle.Turtle()
choice_2.speed(0)
choice_2.color('yellow')
choice_2.penup()
choice_2.hideturtle()
choice_2.goto(0,75)
choice_2.write("2. Get Name of Element",align="center",font=('Courier New',15,'normal'))

choice_3 = turtle.Turtle()
choice_3.speed(0)
choice_3.color('purple')
choice_3.penup()
choice_3.hideturtle()
choice_3.goto(0,50)
choice_3.write("3. Get Discoverer of Element",align="center",font=('Courier New',15,'normal'))

choice_4 = turtle.Turtle()
choice_4.speed(0)
choice_4.color('pink')
choice_4.penup()
choice_4.hideturtle()
choice_4.goto(0,25)
choice_4.write("4. Get Number of Electrons in Element",align="center",font=('Courier New',15,'normal'))


choice_5 = turtle.Turtle()
choice_5.speed(0)
choice_5.color('red')
choice_5.penup()
choice_5.hideturtle()
choice_5.goto(0,0)
choice_5.write("5. Get Ionization Energy of Element",align="center",font=('Courier New',15,'normal'))

choice_6 = turtle.Turtle()
choice_6.speed(0)
choice_6.color('green')
choice_6.penup()
choice_6.hideturtle()
choice_6.goto(0,-25)
choice_6.write("6. Get Mass Number of Element",align="center",font=('Courier New',15,'normal'))


choice_7 = turtle.Turtle()
choice_7.speed(0)
choice_7.color('blue')
choice_7.penup()
choice_7.hideturtle()
choice_7.goto(0,-50)
choice_7.write("7. Get Name Origin",align="center",font=('Courier New',15,'normal'))

choice_8 = turtle.Turtle()
choice_8.speed(0)
choice_8.color('yellow')
choice_8.penup()
choice_8.hideturtle()
choice_8.goto(0,-75)
choice_8.write("8. Get electronic Configuration",align="center",font=('Courier New',15,'normal'))





        

        


        
            







if __name__ == '__main__':
    while True:
        window.update()
        window.onscreenclick(getCoords)

我是python海龟库的初学者。我认为错误基本上是由于while循环。如果有更好的方法在海龟中设计菜单,那么也请发布。

编辑:仅当我单击指定给每个按钮的区域时才会发生错误

标签: pythonpython-turtle

解决方案


推荐阅读