我正在处理以下程序,我无法从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 :(得分:1)
您应该将global data
移到data = scrape.scrape_info(item,loc)
上方的行,因为没有它,分配会在data
的范围内创建一个新的displayPage
变量__init__
方法。
见这个非常基本的例子:
def foo():
global a
a = 1
def bar():
print(a)
foo()
bar()
>> 1
如果我们从global a
删除foo
,即
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
答案 1 :(得分:0)
您需要在global
方法中使用search
关键字来访问全局data
。
def search(item,loc,cont):
global data