Python 从其他 class 获取属性到另一个 class
Python getting attribute from other class to another class
在 Python 中构建应用程序还很陌生,我不知道如何将参数从其他 class 函数传递给其他 class。根据我阅读的内容,我想我应该在 CustomPopup class 中使用 def init 但无法使其正常工作。
这是我正在尝试做的事情的精简版本,这有效,但我试图将 txt_input 从 popuper 获取到 CustomPopup 但无法使其工作:
Python:
from kivy.app import App
from kivy.lang import Builder
from os import listdir
from kivy.uix.screenmanager import Screen, ScreenManager
from kivy.uix.popup import Popup
import teradata
import Global
class CustomPopup(Popup):
txt_input = 'Text'
udaExec = teradata.UdaExec()
kv_path = './kv/'
for kv in listdir(kv_path):
Builder.load_file(kv_path+kv)
class LoginScreen(Screen):
def buttonClicked(self):
Global.u_name = self.ids.u_name.text
Global.p_word = self.ids.p_word.text
status = None
try:
with udaExec.connect("${dataSourceName}",username=self.ids.u_name.text,password=self.ids.p_word.text) as session:
try:
session
except teradata.DatabaseError as e:
status = e.code
except teradata.DatabaseError as e:
status = e.code
if status == 8017:
self.popuper('Hello')
elif status == None:
self.popuper('All Good')
elif status == 0:
self.popuper('Fill in username')
else:
self.popuper('Something else')
def popuper(self, txt_input):
popuper = CustomPopup()
popuper.open()
class MainScreen(Screen):
pass
class ScreenManagement(ScreenManager):
pass
application = Builder.load_file("main.kv")
class MainApp(App):
def build(self):
self.title = 'Push Notfication Tool'
return application
if __name__ == "__main__":
MainApp().run()
基维:
<CustomPopup@Popup>:
size_hint: (.5,.5)
auto_dismiss: False
title: "af"
BoxLayout:
orientation: 'vertical'
Label:
text: root.txt_input
Button:
text: "Close now"
on_press: root.dismiss()
谢谢!
*EDIT 也添加了 kivy 文件。
尝试改变一些东西:
class CustomPopup(Popup):
txt_input = StringProperty('Text')
def __init__(self, txt_input, **kw):
super().__init__(**kw)
self.txt_input = txt_input
...
def popuper(self, txt_input):
popuper = CustomPopup(txt_input) # send txt_input!
popuper.open()
现在是 kv 文件
<CustomPopup>: # removed @Popup since you created it in python...
size_hint: (.5,.5)
auto_dismiss: False
title: "af"
BoxLayout:
orientation: 'vertical'
Label:
text: root.txt_input
Button:
text: "Close now"
on_press: root.dismiss()
在 Python 中构建应用程序还很陌生,我不知道如何将参数从其他 class 函数传递给其他 class。根据我阅读的内容,我想我应该在 CustomPopup class 中使用 def init 但无法使其正常工作。
这是我正在尝试做的事情的精简版本,这有效,但我试图将 txt_input 从 popuper 获取到 CustomPopup 但无法使其工作:
Python:
from kivy.app import App
from kivy.lang import Builder
from os import listdir
from kivy.uix.screenmanager import Screen, ScreenManager
from kivy.uix.popup import Popup
import teradata
import Global
class CustomPopup(Popup):
txt_input = 'Text'
udaExec = teradata.UdaExec()
kv_path = './kv/'
for kv in listdir(kv_path):
Builder.load_file(kv_path+kv)
class LoginScreen(Screen):
def buttonClicked(self):
Global.u_name = self.ids.u_name.text
Global.p_word = self.ids.p_word.text
status = None
try:
with udaExec.connect("${dataSourceName}",username=self.ids.u_name.text,password=self.ids.p_word.text) as session:
try:
session
except teradata.DatabaseError as e:
status = e.code
except teradata.DatabaseError as e:
status = e.code
if status == 8017:
self.popuper('Hello')
elif status == None:
self.popuper('All Good')
elif status == 0:
self.popuper('Fill in username')
else:
self.popuper('Something else')
def popuper(self, txt_input):
popuper = CustomPopup()
popuper.open()
class MainScreen(Screen):
pass
class ScreenManagement(ScreenManager):
pass
application = Builder.load_file("main.kv")
class MainApp(App):
def build(self):
self.title = 'Push Notfication Tool'
return application
if __name__ == "__main__":
MainApp().run()
基维:
<CustomPopup@Popup>:
size_hint: (.5,.5)
auto_dismiss: False
title: "af"
BoxLayout:
orientation: 'vertical'
Label:
text: root.txt_input
Button:
text: "Close now"
on_press: root.dismiss()
谢谢!
*EDIT 也添加了 kivy 文件。
尝试改变一些东西:
class CustomPopup(Popup):
txt_input = StringProperty('Text')
def __init__(self, txt_input, **kw):
super().__init__(**kw)
self.txt_input = txt_input
...
def popuper(self, txt_input):
popuper = CustomPopup(txt_input) # send txt_input!
popuper.open()
现在是 kv 文件
<CustomPopup>: # removed @Popup since you created it in python...
size_hint: (.5,.5)
auto_dismiss: False
title: "af"
BoxLayout:
orientation: 'vertical'
Label:
text: root.txt_input
Button:
text: "Close now"
on_press: root.dismiss()