2013-05-20 37 views
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功能我也把它定义但它不承认它。

任何人有任何建议,因为我难住?

回答

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

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

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