Reputation: 71
So, I will go straight to the point, and will insert code at the end, but the main problem is that I'm working on a python file (using kivy for a pseudo GUI), the thing is that i have separate files for each screen so they will work as separate apps also and what i haven't been able is to pass information from one class imported to another, even when they actually are from the same file. Have read similar question and already understand how to pass variable from one to another in the same code, but could't yet get to make this work.
here is the code, the important part at least;
contacts.py:
import kivy
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.button import Button
from kivy.uix.label import Label
from kivy.uix.screenmanager import Screen
from kivy.lang import Builder
from functools import partial
import time
from neo4j.v1 import GraphDatabase, basic_auth
neodb = GraphDatabase.driver("bolt://localhost:7687", auth=basic_auth("neo4j", "**********"))
neosession = neodb.session()
kivy.require("1.9.0")
Builder.load_file("Apps/Contacts.kv")
class ContactScreen(Screen):
cid = ""
def __init__(self, **kwargs):
super(ContactScreen, self).__init__(**kwargs)
self.bind(on_enter=self.contactlayout)
def contactlayout(self, *args):
global neosession
contacts = neosession.run("MATCH (a:Person) RETURN a.Name as name, a.Lastname as lastname, a.id as id ORDER"
" BY a.Lastname")
mainlayout = BoxLayout(orientation="vertical")
buttonlayout = BoxLayout(orientation="horizontal", size_hint=(1, None), height=22)
listlayout = BoxLayout(orientation="vertical")
screenlabel = Label(text="Contactos", size_hint=(1, None), height=20, font_size=20)
newcontact = Button(text="+", size_hint=(None, None), size=(20, 20), font_size=20)
newcontact.bind(on_release=self.tonewcontact)
for contact in contacts:
contactlbl = Button(text=contact["lastname"] + " " + contact["name"], size_hint=(1, None), height=26,
font_size=18, text_size=self.size, halign="left", valign="middle")
idn = contact["id"]
contactlbl.bind(on_press=partial(self.tocontact, contact["id"]))
listlayout.add_widget(contactlbl)
bottom = Label()
listlayout.add_widget(bottom)
buttonlayout.add_widget(screenlabel)
buttonlayout.add_widget(newcontact)
mainlayout.add_widget(buttonlayout)
mainlayout.add_widget(listlayout)
self.add_widget(mainlayout)
def tonewcontact(self, *args):
self.manager.current = "NewContactScreen"
def tocontact(self, cidn, *args):
global cid
cid = cidn
# print(cid)
self.manager.current = "SeeContactScreen"
class NewContactScreen(Screen):
def __init__(self, **kwargs):
super(NewContactScreen, self).__init__(**kwargs)
def cancel(self, *args):
self.manager.current = "ContactScreen"
def submit(self, setname, setlastname, setnumber, setemail):
global neosession
if setname != "":
neosession.run("CREATE (a:Person {Name: {name}, Lastname: {lastname}, Number: {number}, Email: {email}, "
"id: {id}})",
{"name": setname, "lastname": setlastname, "number": setnumber, "email": setemail,
"id": time.strftime("%Y%m%d%H%M%S")})
self.ids.name.theTxt.text = ""
self.ids.lastname.theTxt.text = ""
self.ids.number.theTxt.text = ""
self.ids.email.theTxt.text = ""
self.manager.current = "ContactScreen"
else:
pass
class SeeContactScreen(Screen):
def __init__(self, **kwargs):
super(SeeContactScreen, self).__init__(**kwargs)
self.bind(on_enter=self.contactlayout)
def contactlayout(self, *args):
print(ContactScreen.cid)
Brain.py:
import kivy
from kivy.app import App
from kivy.core.window import Window
from kivy.uix.screenmanager import ScreenManager, Screen, NoTransition
from kivy.clock import Clock
import time
from neo4j.v1 import GraphDatabase, basic_auth
from Apps.OpenScreen import OpenScreen
from Apps.Calculator import CalculatorScreen
from Apps.Contacts import ContactScreen
from Apps.Contacts import NewContactScreen
from Apps.Contacts import SeeContactScreen
neodb = GraphDatabase.driver("bolt://localhost:7687", auth=basic_auth("neo4j", "**********"))
neosession = neodb.session()
kivy.require("1.9.0")
__author__ = 'enzocanessa'
Window.clearcolor = (1, .5, 0, 1)
Window.size = (480, 320)
# Window.fullscreen = True
Window.show_cursor = True
class MenuScreen(Screen):
def display_time(self):
Clock.schedule_interval(self.give_time, 1)
def give_time(self, *args):
self.display.text = time.strftime("%a %Y-%m-%d %H:%M:%S")
class BrainApp(App):
def build(self):
m = ScreenManager(transition=NoTransition())
m.add_widget(OpenScreen(name="open_screen"))
m.add_widget(MenuScreen(name="MenuScreen"))
m.add_widget(CalculatorScreen(name="CalculatorScreen"))
m.add_widget(SeeContactScreen(name="SeeContactScreen"))
m.add_widget(NewContactScreen(name="NewContactScreen"))
m.add_widget(ContactScreen(name="ContactScreen"))
return m
if __name__ == "__main__":
BrainApp().run()
and obviously will by thankful for any help that you can bring.
Upvotes: 0
Views: 144
Reputation: 13347
You should be able to read the variable in SeeContactScreen
with the following way :
-add an attribute to the class:
class SeeContactScreen(Screen):
def __init__(self, **kwargs):
super(SeeContactScreen, self).__init__(**kwargs)
self.cid = None
self.bind(on_enter=self.contactlayout)
-and change this attribute when you call the screen :
def tocontact(self, cidn, *args):
self.manager.get_screen('SeeContactScreen').cid = cidn
self.manager.current = "SeeContactScreen"
Upvotes: 1