分配前的局部变量参考

时间:2013-05-20 03:15:37

标签: python user-interface tkinter python-3.3

好的,我正在使用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函数中我确实定义了它,但它无法识别它。

任何人都有任何建议因为我难倒?

1 个答案:

答案 0 :(得分:2)

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

如果cardName 等于“”,会发生什么?

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