2016-07-27 3 views
0

私は以下のプログラムで作業中です。displayPageクラス内の検索メソッドからデータ(そのデータフレーム)オブジェクトにアクセスできません。グローバルキーワードを使ってみました。動作していないようだ、誰も私にこれを助けることができますか?クラス内のどこにでもアクセスできるようにグローバルなデータフレームを作成

import tkinter as tk 
import tkinter.messagebox as tm 
from tkinter import * 
from tkinter import ttk 
import sys 
import pandas as pd 

global data 

#importing our other .py file whihc is used for scraping infomration from the webpages 
import scrape 

FONTT = ("Times", "12", "bold italic") 

class myApp(tk.Tk): 
    def __init__(self,*args,**kwargs): 
     tk.Tk.__init__(self,*args,**kwargs) 

     tk.Tk.wm_title(self,"Python Project") 
     container = tk.Frame(self) 
     container.pack(side = "top", fill = "both", expand = True) 
     container.grid_rowconfigure(0,weight = 1) 
     container.grid_columnconfigure(0,weight = 1) 

     self.frames = {} 
     for F in (LoginPage, SearchPage,displayPage): 
      frame = F(container,self) 
      self.frames[F] = frame 
      frame.grid(row = 0,column = 0,sticky = "nsew") 

     self.show_frame(LoginPage) 

    #Function to show the page required thorugh navigation in the application 
    def show_frame(self,cont): 
     frame = self.frames[cont] 
     frame.tkraise()  


#Function to validate the username and password entered by the user 

def loginValidate(user,pwd,cont): 
    if(user == "yogesh" and pwd == "123456"): 
     cont.show_frame(SearchPage) 
    else: 
     tm.showerror("Login error", "Incorrect username or password") 

#Function for fetching the dataframe containing the scraped infomration 
def search(item,loc,cont): 
    **data = scrape.scrape_info(item,loc)** 
    cont.show_frame(displayPage) 

class LoginPage(tk.Frame): 

    def __init__(self,parent,controller): 
     tk.Frame.__init__(self,parent) 
     usr_login = StringVar() 
     pwd_login = StringVar() 
     userLabel = tk.Label(self,text = "Name",font = FONTT) 
     passwordLabel = tk.Label(self,text = "Password", font = FONTT) 

     userEntry = tk.Entry(self, textvariable = usr_login, bd=5) 
     passwordEntry = tk.Entry(self, textvariable=pwd_login,bd=5,show = "*") 

     submitButton = ttk.Button(self,text = "Login",command = lambda: loginValidate(usr_login.get(),pwd_login.get(),controller)) 
     quitButton = ttk.Button(self,text = "Quit",command = self.exit) 


     userLabel.grid(row = 0,sticky = "E",padx =10,pady =10) 
     passwordLabel.grid(row =1,sticky = "E",padx =10,pady =10) 
     userEntry.grid(row=0,column=1,padx =10,pady =10) 
     passwordEntry.grid(row=1,column=1,padx =10,pady =10) 
     submitButton.grid(row =2,column =1,padx =10,pady =10) 
     quitButton.grid(row=2,column=2,padx =10,pady =10) 

    def exit(self): 
     exit() 

class SearchPage(tk.Frame): 
    def __init__(self,parent,controller): 
     tk.Frame.__init__(self,parent) 
     welcomeLabel = tk.Label(self,text = "Welcome User", font = FONTT) 
     logoutButton = ttk.Button(self,text = "Logout", command = lambda: controller.show_frame(LoginPage)) 

     item_search = StringVar() 
     loc_search = StringVar()   

     item = tk.Label(self,text = "Find?") 
     location = tk.Label(self,text = "Location?") 
     itemSearch = tk.Entry(self,bd =5,textvariable = item_search) 
     locSearch = tk.Entry(self,bd =5,textvariable = loc_search) 

     searchButton = ttk.Button(self,text = "Search",command = lambda: search(item_search.get(),loc_search.get(),controller)) 

     welcomeLabel.grid(row = 0) 
     logoutButton.grid(row = 0,column =2) 



     item.grid(row=1,column=0,padx =10,pady =10) 
     location.grid(row=2,column=0,padx =10,pady =10) 
     itemSearch.grid(row=1,column=1,padx =10,pady =10) 
     locSearch.grid(row=2,column=1,padx =10,pady =10) 
     searchButton.grid(row=3,column=1,padx =10,pady =10) 

class displayPage(tk.Frame): 
    def __init__(self,parent,controller): 
     tk.Frame.__init__(self,parent) 
     for i in range(25): 
      **lable = tk.Label(self,text = str(data.business_name[i]),padx =10,pady =10)** 
      lable.pack() 


app = myApp() 
app.mainloop() 
+0

この回答は役に立ちますか? http://stackoverflow.com/a/32213127/7432「コントローラにデータを格納する」の節を参照してください –

答えて

0

それなしで、割り当てがdisplayPage__init__法の適用範囲に新しいdata変数を作成しますので、あなたは、data = scrape.scrape_info(item,loc)上の行にglobal dataを移動する必要があります。私たちはすなわち

def foo(): 
    a = 1 

def bar(): 
    print(a) 

foo() 
bar() 
>> Traceback (most recent call last): 
    File "main.py", line 33, in <module> 
     ba() 
    File "main.py", line 30, in bar 
     print(a) 
    NameError: name 'a' is not defined 
+0

意味がありますが、私の場合は動作しません。理由はわかりませんが、エラーが表示されるたびに"名前データが定義されていません"。 – YogeshReddy

0

fooからglobal aを削除する場合は、グローバルdataにアクセスするためにsearch方法でglobalキーワードを使用する必要が

def foo(): 
    global a 
    a = 1 

def bar(): 
    print(a) 

foo() 
bar() 
>> 1 

は、この非常に基本的な例を参照してください。

def search(item,loc,cont): 
    global data 
+0

この回答は役に立ちましたか? – Zety

関連する問題