Reputation: 23
I am working on the below program, I am unable to access the data (its a dataframe) object from the search method inside the displayPage class. I tried using the global key word. Doesn't seems to work, can anyone help me out with this?
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()
Upvotes: 0
Views: 4257
Reputation: 26688
You need to use global
keyword in the search
method to access global data
.
def search(item,loc,cont):
global data
Upvotes: 0
Reputation: 81594
You should move global data
to the line above data = scrape.scrape_info(item,loc)
, since without it, the assignment creates a new data
variable in the scope of displayPage
's __init__
method.
See this very basic example:
def foo():
global a
a = 1
def bar():
print(a)
foo()
bar()
>> 1
If we remove global a
from foo
, ie
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
Upvotes: 1