所以,我将直截了当地说,并将在最后插入代码,但主要的问题是我正在处理一个python文件(使用kivy作为伪GUI),事情就是我有每个屏幕都有单独的文件,因此它们也可以作为单独的应用程序工作,而我无法将信息从一个类导入到另一个类,即使它们实际上来自同一个文件。已经阅读了类似的问题,并且已经了解了如何在同一个代码中将变量从一个传递到另一个,但还不能使其工作。
这里是代码,至少是重要的部分;
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()
显然会感谢您提供的任何帮助。
答案 0 :(得分:1)
您应该能够通过以下方式阅读SeeContactScreen
中的变量:
- 为该类添加一个属性:
class SeeContactScreen(Screen):
def __init__(self, **kwargs):
super(SeeContactScreen, self).__init__(**kwargs)
self.cid = None
self.bind(on_enter=self.contactlayout)
- 并在调用屏幕时更改此属性:
def tocontact(self, cidn, *args):
self.manager.get_screen('SeeContactScreen').cid = cidn
self.manager.current = "SeeContactScreen"