2013-05-20 74 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應該發生的情況,並相應地處理該情況。