[英]Python, Kivy: Problem with calling functions from different classes/screens
我無法正確調用來自不同類的函數。
我正在制作一個簡單的游戲,它使用通關所需的時間來計算分數。 有一個秒表在后台運行,我想在彈出菜單中添加一個暫停按鈕,並在這個彈出菜單中添加一個恢復按鈕。
問題是當從彈出菜單中調用暫停 function 時,它也會在彈出菜單中返回,而不是在主窗口小部件中。
這是代碼的簡化版本:
import kivy
from kivy.app import App
from kivy.lang import Builder
from kivy.properties import NumericProperty
from kivy.uix.widget import Widget
from kivy.uix.popup import Popup
from kivy.clock import Clock
root_widget = Builder.load_file('app.kv')
class ExampleWidget(Widget):
time = NumericProperty(0)
paused = False
stop = False
# Keeping time
def increment_time(self, interval):
self.time += .1
print(self.time) # To check if stopwatch is running or not
# Stop should mean that the stopwatch must reset when it starts again.
# When paused it should resume when it starts again
def stop_start_or_pause(self):
# stop stopwatch
if self.stop:
Clock.unschedule(self.increment_time)
print('Stopped')
# Make sure time is 0 when restarting
elif not self.stop and not self.paused:
# Keeping time
self.time = 0
Clock.schedule_interval(self.increment_time, .1)
# Pause stopwatch
elif self.paused:
Clock.unschedule(self.increment_time)
print("!!", self.time) # To make it easier to see if stopwatch actually resumes where it left off
print('unscheduled') # Just to confirm and to make it a bit easier to see
# resume stopwatch
elif not self.paused:
Clock.schedule_interval(self.increment_time, .1)
class PopupMenu(Popup):
example = ExampleWidget()
class MyApp(App):
ExampleWidget = ExampleWidget()
def build(self):
return ExampleWidget()
MyApp().run()
.kv 文件:
#:import Factory kivy.factory.Factory
<PopupMenu@Popup>
auto_dismiss: False
size_hint_y: .8
size_hint_x: .9
title: 'Pause'
example: app.ExampleWidget
BoxLayout:
Button:
text: 'resume'
on_press: root.example.paused = False
on_release: root.dismiss(); root.example.stop_start_or_pause()
size: self.size
<ExampleWidget>:
GridLayout:
col: 2
rows: 3
size: root.size
Button:
text: 'start'
size: self.size
on_press: root.stop = False; root.stop_start_or_pause()
Button:
text: 'stop'
size: self.size
on_press: root.stop = True; root.stop_start_or_pause()
Button:
text: 'Pause menu'
size: self.size
on_press: root.paused = True
on_release: Factory.PopupMenu().open(); root.stop_start_or_pause()
Label:
text: str(round(root.time))
size: self.size
我嘗試制作 function 並使用 Clock.schedule.interval() 繼續檢查是否暫停 == True,但它一直返回:
AttributeError: 'float' object has no attribute 'stopped'
無論如何,這似乎不是有效的解決方案,所以我不想在這個 function 上花費太多時間。 我還試圖找出“愚蠢”的錯誤(即“,”而不是“。”),但那是在我意識到恢復按鈕返回“第二”秒表而不是更新我真正想要使用的秒表之前。
我希望有人可以提供幫助,並且我的問題很清楚。 英語不是我的第一語言,所以我有時很難找到解釋/提問的最佳方式。
先感謝您!
如果我理解您的問題,那么問題出在您的MyApp
class 上:
class MyApp(App):
ExampleWidget = ExampleWidget()
def build(self):
return ExampleWidget()
此代碼正在創建ExampleWidget
的兩個實例。 一個在build()
方法中返回,一個保存為MyApp
的ExampleWidget
屬性。 現在,當您使用MyApp
的ExampleWidget
屬性時,您沒有引用作為 GUI 根目錄的ExampleWidget
,因此它對屏幕上顯示的內容沒有影響。 修復方法是只創建一個ExampleWidget
實例,如下所示:
class MyApp(App):
ExampleWidget = ExampleWidget()
def build(self):
return self.ExampleWidget
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.