2017-08-15 20 views
0

最後にコードを挿入しますが、主な問題はPythonファイル(kivyを擬似GUIに使用しています)実際には同じファイルのものであっても、別のアプリケーションとしても動作するように、各画面ごとに別々のファイルがあるということです。同様の質問を読んで、同じコード内の変数を別のものに渡す方法をすでに理解していますが、まだこの作業を行うことはできませんでした。2つのインポートされたクラス間でPythonパス変数を使用する

ここにコードは、少なくとも重要な部分です。

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

あなたが共有したい変数は何ですか、「neosession」ですか? – PRMoureu

+0

nope、選択された連絡先のID – Ecangis

+0

これはPythonについてはあまり分かりませんが、 'kivy'についてはもっとそうです。このようなコメントでこのような質問を見ましたか? https://stackoverflow.com/questions/34080020/passing-variables-between-two-screen-classes-in-kivy?rq=1#comment55960262_34080020 – Cireo

答えて

1

あなたは次のようにSeeContactScreenで変数を読むことができる必要があります:

は、クラスに属性を-add:あなたは画面を呼び出すとき

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" 
+0

チャームのように動作します、ありがとう – Ecangis

関連する問題