python - How do I fix the screen not being displayed after function?
问题描述
I want to save an object and then move to the next screen. The code I have only saves the object and remains on the same page. My main problem I think, is being able to connect my kv code and python code. I think the program doesn't know what the variable for ScreenTwo is. How would I fix this? This is the code that is probably affecting it:
class MainScreen(Screen):
pass
class ScreenOne(Screen):
pass
class ScreenTwo(Screen):
pass
class ScreenManagement(ScreenManager):
main_screen = ObjectProperty(None)
screen_one = ObjectProperty(None)
screen_two = ObjectProperty(None)
presentation = Builder.load_file("StreakStar.kv")
class MainApp(App):
def build(self): # build() returns an instance
self.store = JsonStore("streak.json")
return presentation
def change_screen(self, *args):
ScreenManagement.current = "ScreenTwo"
def display_btn(self):
obj = self.root.get_screen('one')
for key in self.store:
streak_button = Button(text=key)
obj.ids.streak_zone.add_widget(streak_button)
# creates the Streak object
def create(self):
obj = self.root.get_screen('one') # get info from ScreenOne
self.streak = Streak(obj.ids.action_entry.text, obj.ids.streak_entry.text,
obj.ids.day_entry.text, obj.ids.hour_entry.text,
obj.ids.minute_entry.text)
empty_error = "Make sure to fill out all boxes!"
popup = Popup(title="Not filled", content=Label(text=empty_error),
size_hint=(None, None), size=(300, 100))
# error handling and calculating total seconds
parsed = False
try:
total = ((int(self.streak.day) * 86400) + (int(self.streak.hour) * 3600) +
(int(self.streak.minute) * 60)) # convert into seconds
parsed = True
# delete later and replace with return
print("[seconds:", total,']' , "[action:", self.streak.action,']',
"[action number:", self.streak.action_num,']')
self.store.put(self.streak.action, action=self.streak.action,
action_num=self.streak.action_num, seconds=total,
score=self.streak.score)
self.change_screen(self)
except ValueError as error:
popup.open()
.kv code:
ScreenManagement:
id: screen_manager
main_screen: main_screen
screen_one: screen_one
screen_two: screen_two
transition: SlideTransition()
MainScreen:
id: main_screen
name: "main"
manager: screen_manager
ScreenOne:
id: screen_one
name: "one"
manager: screen_manager
ScreenTwo:
id: screen_two
name: "two"
manager: screen_manager
...
Button: # this button is apart of ScreenOne
text: "Add"
size: 50, 25
size_hint: None, None
font_size: 18
on_press: app.create()
<ScreenTwo>
id: screen_two
name: "two"
GridLayout:
cols: 2
rows: 1
BoxLayout:
id: streak_zone
orientation: 'vertical'
AnchorLayout:
anchor_x: "center"
anchor_y: "center"
Label:
id: high_lable
text: "Highest Streak: "
size_hint: None, None
font_size: 20
解决方案
切换画面
调用方法后create()
,如果你想去ScreenTwo
然后替换ScreenManagement.current = "ScreenTwo"
为self.root.current = 'two'
片段
def change_screen(self, *args):
self.root.current = "two"
在另一个屏幕中访问属性
由于您已使用 ObjectProperties 链接屏幕,因此您可以通过两种方法访问 ScreenTwo 中的变量。
方法一
使用self.root.screen_one.ids.id_name
或self.root.screen_two.ids.id_name
片段
self.root.screen_one.ids.action_entry.text
self.root.screen_one.ids.streak_entry.text
self.root.screen_one.ids.day_entry.text
self.root.screen_one.ids.hour_entry.text
self.root.screen_one.ids.minute_entry.text
self.root.screen_two.ids.streak_zone
self.root.screen_two.ids.high_lable.text
方法二
您已经在使用其中之一,即obj = self.root.get_screen('one')
推荐阅读
- pdf - 使用 openPDF 添加认证/docMDP 签名
- javascript - 仅使用名称属性检索输入值(VueJS 嵌套表单)
- flutter - 它不会回调到 purchaseUpdated 函数
- java - 信任服务器上所有客户端证书的安全隐患 (Java X509TrustManager)
- python - 为什么“import rpy2”有效但“import rpy2.robjects”引发错误?
- javascript - canvas touchmove 有按钮在手机浏览器上无法使用的效果
- java - 如何在 Raspberry Pi 上使用 Java 访问“user.home”并写入它?
- python - 循环中的两个python脚本,另一个有计时器,而另一个没有
- javascript - 如何解决 FirebaseError:collection() 的第一个参数是 CollectionReference、DocumentReference 或 FirebaseFirestore 问题?
- python - 日期以正确的时间存储但没有 TZ