0

好的,所以我正在使用 Tkinter (3.3) 制作一个程序。用户提交一个姓名,然后在数据库中搜索该姓名,该数据库cards.cdb打印出其信息,然后将其与在线资源进行比较url。它还使用函数显示命名事物的图片picture。因此,当单击按钮时,它会调用该buttonclicked函数,然后该函数会调用另外两个函数。

from tkinter import *
import urllib
import urllib.request
from bs4 import BeautifulSoup
import Pydeck
import sys
from collections import defaultdict

root = Tk()
name=""
def buttonclicked():
    name()
    picture(text)

def name():
    all_lists=[] #all lists
    text = inputfield.get()
    Pydeck.loadDatabase('C://Users/Trevor/Desktop/Y-In Process/cards.cdb')
    cardName = Pydeck.getCardsFromName(text)
    if not cardName == "":
            c = Pydeck.Card(cardName)
    tex.insert(END, c.name)
    level="\nLevel %s" % c.level + " " + c.attribute + " " + c.typestring 
    tex.insert(END, level)
    atk="\nAtk: %s" % c.attack
    tex.insert(END, atk)
    defe="\nDef: %s" % c.defense
    tex.insert(END, defe)
    typestring='\n%s' %c.typestring
    desc='\n%s' %c.description
    seperator='\n--------------------\n'
    tex.insert(END, typestring)
    tex.insert(END, desc)
    tex.insert(END,seperator)
    #--
    url_name=c.name.replace(' ','_')
    url=('http://yugioh.wikia.com/wiki/Card_Tips:{}'.format(url_name))
    page = urllib.request.urlopen(url)
    soup = BeautifulSoup(page.read())
    content = soup.find('div',id='mw-content-text')
    links = content.findAll('a')
    link_lists = defaultdict(list)
    all_lists.append([link.get("title") for link in links])
    common_links = set(all_lists[0]).intersection(*all_lists[1:])
    omit_list=['None', 'Special Summon', 'Edit Searchable by section', 'Edit Special Summoned from the hand by section','Edit Special Summoned from the Deck by section','Edit Special Summoned from the Graveyard by section','Edit Easier Tribute Summons section','Edit Generic Tips section','Edit Traditional Format section']
    final=set(common_links)-set(omit_list)
    tex.insert(END,final)
    #--
    tex.see(END)             # Scroll if necessary


def picture(text):
    gifdir = "C:\\Users\\Trevor\\Desktop\\Y-In Process\\Pictures\\"
    Pydeck.loadDatabase('C://Users/Trevor/Desktop/Y-In Process/cards.cdb')
    cardName = Pydeck.getCardsFromName(text)
    if not cardName == "":
        c=Pydeck.Card(cardName)
    filename='{}.gif' .format(c.cardID)
    img = PhotoImage(file=gifdir+filename)
    can = Canvas(root)
    can.pack(fill=BOTH,side='left')
    can.config(width=img.width(), height=img.height())        
    can.create_image(2, 2, image=img, anchor=NW)

tex=Text(root)
tex.pack(side='right')
inputfield = Entry(root)
inputfield.pack(side='bottom')
but = Button(root,text="Enter Name", command = buttonclicked) #Calls name function
but.pack(side='bottom')
text = inputfield.get()


root.mainloop()

我收到错误

filename='{}.gif' .format(c.cardID)
UnboundLocalError: local variable 'c' referenced before assignment

我知道这意味着c没有被创建,但在picture函数中我确实定义了它,但它无法识别它。

有人有什么建议,因为我很难过吗?

4

1 回答 1

2
if not cardName == "":
    c=Pydeck.Card(cardName)
filename='{}.gif' .format(c.cardID)

如果等于 "" 会发生cardName 什么

由于这是唯一c初始化的地方,如果cardName是空的,那么你会看到这个错误。您需要定义应该发生的事情cardName是空的,并相应地处理该条件。

于 2013-05-20T03:17:20.747 回答