I'm trying to use Kivy
to add a GUI to some script. However, the following is stalling me significantly:
class SearchScreen(Screen):
search_input = ObjectProperty()
users_dic = {}
def search_user(self):
globals()["users_dic"] = GetSessionToken.find_user(
self.search_input.text, agent_head)
print globals()['users_dic']
users = [key for key in users_dic.iterkeys()]
self.search_results.item_strings = users
self.search_results.adapter.data[:]
self.search_results.adapter.data.extend(users)
self.search_results._trigger_reset_populate()
class userButton(ListItemButton):
pass
def getUserSession(self, user):
global current_user, user_header, session_token
current_user = user
users_dic = globals()['users_dic']
print users_dic
try:
user_header, session_token = GetSessionToken.UserSession(
users_dic[user], agent_head)
return self.root.changeScreen("operations")
except ValueError:
return self.root.changeScreen("denied")
It produces this error:
KeyError: 'users_dic'
I do not understand why this is working for "search_user"
but not for "getUserSession"
. To confirm, "search_user" prints out the dictionary:
{u'user1': u'202703', u'user2': u'202702', u'user3': u'201105'}
I want to be provided with "202703" when I run "getUserSession('user1')". What am I missing about Globals
? What are the differences between the way the functions are handling "users_dic"?
EDIT:
search_user
is being called in the Kivy language file as follows:
<SearchScreen@Screen>:
search_input: search_box
search_results: search_results_list
BoxLayout:
orientation: "vertical"
BoxLayout
height: root.height/10
size_hint_y: None
TextInput:
id: search_box
size_hint_x: 75
Button:
text:"Search"
size_hint_x: 25
on_press: root.search_user()
getUserSession
is being called within the Kivy language file as follows:
<userButton>:
on_press: main.SearchScreen().getUserSession(self.text)
In search_user
you have set a value to Globals["users_dic"]
, therefore you were able to access it, when your code reached getUserSession
user_dic
is not yet defined in Globals
.
If you had called search_user
prior to calling getUserSession
, you should be able to access users_dic
as part of the Globals
, like shown in this example:
class Foo():
def test(self):
globals()['test'] = 'test'
def post_test(self):
print globals()['test']
foo = Foo()
foo.test()
foo.post_test()
>>test
Just to be clear, setting users_dic
in the class scope doesn't make it part of Globals
.
For anybody who may stumble across an issue similar to mine, I worked around the problem by moving the function to be within the root widget, and changing the event to reference this instead.
This means moving the function from being within:
class SearchScreen(Screen):
to being under:
class AgentAppRoot(BoxLayout):
Then changing the event from:
on_press: root.getUserSession(self.text)
to:
on_press: main.SearchScreen().getUserSession(self.text)
I am unsure as to why this has worked, but it has... for now. For anybody in my position I regret I am unable to offer anything further.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.