我想將main.py中的計時器的值傳遞給exit.kv文件中的ProgressBar,但使用文件結構(如showcase示例)。我試圖製作一個定時按鈕,這樣程序不會意外退出,我需要按住按鈕幾秒鐘。從py文件到kv文件的值。使用展示示例
已經嘗試了很多東西,但我找不到解決方案。
該項目需要(使用我的樹莓派一個DAC)通過在main.py更多價值,以幾個不同的.kv文件
這是我第Kivy項目,但也是我的第一個Python項目,所以學習曲線對我來說很陡峭。
編輯:
我需要的是在main.py文件中exit.kv文件傳遞(exit_time)值的值的進度(exit_bar)以顯示在退出程序之前需要多長時間按下按鈕(exit_button)。
程序運行時,蟒蛇控制檯會做倒計時,但我不能讓它的進度顯示(或標籤上的事exit.kv文件。
如何找到,如果我需要使用的應用程序或根或screen.ids.exit_bar.value或者什麼又
編輯:。。?解決方案
我刪除了有關計時器在main.py一切,把這個進入main.py;
#
# The Exit Timer, counting up to 20 from 0 before exiting.
# If Press button start Timer. If button up, reset timer
#
def my_callback(self, dt):
global exit_time
exit_time += 1
if exit_time == 101:
sys.exit("piMote stopped!")
self.time = exit_time
def start_exit_timer(self):
Clock.schedule_interval(self.my_callback, 0.01)
def stop_exit_timer(self):
global exit_time
Clock.unschedule(self.my_callback)
Clock.schedule_once(self.my_callback,0)
exit_time = -1
然後,我改變了對exit.ky的一部分
ProgressBar:
id: exit_bar
max: 100
value: app.time
size_hint_x: .3
size_hint_y: None
height: '10dp'
在現實中,所有我需要做的是將self.time = exit_time」
main.py
from time import time
from kivy.app import App
from os.path import dirname, join
from kivy.lang import Builder
from kivy.properties import NumericProperty, StringProperty, BooleanProperty,\
ListProperty
from kivy.clock import Clock
from kivy.uix.screenmanager import Screen
from kivy.uix.checkbox import CheckBox
import os
import sys
from kivy.config import Config
exit_time = 0
timer_start = 0
class ShowcaseScreen(Screen):
fullscreen = BooleanProperty(False)
def add_widget(self, *args):
if 'content' in self.ids:
return self.ids.content.add_widget(*args)
return super(ShowcaseScreen, self).add_widget(*args)
class ShowcaseApp(App):
index = NumericProperty(-1)
current_title = StringProperty()
time = NumericProperty(0)
screen_names = ListProperty([])
hierarchy = ListProperty([])
exit_time = NumericProperty(21)
def build(self):
self.title = 'hello world'
self.screens = {}
self.available_screens = [
'Exit']
self.screen_names = self.available_screens
curdir = dirname(__file__)
self.available_screens = [join(curdir,
'{}.kv'.format(fn)) for fn in self.available_screens]
screen = self.load_screen(self.index +1)
self.current_title = screen.name
#
# choose the right screen from spinner
#
def on_current_title(self, instance, value):
self.root.ids.spnr.text = value
def go_screen(self, idx):
print self.index
self.index = idx
self.root.ids.sm.switch_to(self.load_screen(idx), direction='left')
def load_screen(self, index):
if index in self.screens:
return self.screens[index]
screen = Builder.load_file(self.available_screens[index].lower())
self.screens[index] = screen
return screen
#
# timer start for joystick and exit timer
#
def __init__(self,**kwargs):
super(ShowcaseApp,self).__init__(**kwargs)
Clock.schedule_interval(self.my_timer, 0.1)
#
# The Exit Timer, counting up to 20 from 0 before exiting.
# If Press button start Timer. If button up, reset timer
#
def my_timer(screen, dt):
if timer_start == 1:
global exit_time
exit_time += 1
if exit_time == 21:
sys.exit("piMote stopped!")
print exit_time
return exit_time + 7
#################################################################
# Need to do something here to make the timer-progressbar #
# move, counting how long the button is pressed #
#################################################################
def start_exit_timer(self):
global timer_start
timer_start = 1
def stop_exit_timer(self):
global timer_start, exit_time
exit_time = 0
timer_start = 0
if __name__ == '__main__':
ShowcaseApp().run()
showcase.kv
#:kivy 1.8.0
#:import Factory kivy.factory.Factory
<[email protected]>
background_color: .4, .4, .4, 1
<[email protected]+ActionItem>
canvas.before:
Color:
rgba: 0.128, 0.128, 0.128, 1
Rectangle:
size: self.size
pos: self.pos
border: 27, 20, 12, 12
#background_normal: 'atlas://data/images/defaulttheme/action_group'
option_cls: Factory.ActionSpinnerOptions
<ShowcaseScreen>:
ScrollView:
do_scroll_x: False
do_scroll_y: False if root.fullscreen else (content.height > root.height - dp(16))
AnchorLayout:
size_hint_y: None
height: root.height if root.fullscreen else max(root.height, content.height)
GridLayout:
id: content
cols: 1
spacing: '8dp'
padding: '8dp'
size_hint: (1, 1) if root.fullscreen else (.8, None)
height: self.height if root.fullscreen else self.minimum_height
BoxLayout:
orientation: 'vertical'
canvas.before:
Color:
rgb: .6, .6, .6
Rectangle:
size: self.size
#source: 'data/background.png'
ActionBar:
ActionView:
id: av
ActionPrevious:
title: 'piMote'
ActionSpinner:
id: spnr
important: True
text: 'Jump to Screen'
values: app.screen_names
on_text:
if sm.current != args[1]:\
idx = app.screen_names.index(args[1]);\
app.go_screen(idx)
ScreenManager:
id: sm
on_current_screen:
spnr.text = args[1].name
idx = app.screen_names.index(args[1].name)
if idx > -1: app.hierarchy.append(idx)
exit.kv
ShowcaseScreen:
name: 'Exit'
Label:
id: exit_label1
text: "Are you really sure\n you want to quit?"
size_hint_y: None
height: '36dp'
ProgressBar:
id: exit_bar
max: 20
#value: app.my_timer()
size_hint_x: .3
size_hint_y: None
height: '10dp'
Button:
id: exit_button
text: "Yes"
size_hint_x: .2
size_hint_y: None
height: '48dp'
on_press: app.start_exit_timer()
on_release: app.stop_exit_timer()
TL; TR:您可以簡化你的榜樣。發佈你的堆棧跟蹤。 –
我不知道如何簡化我的示例,並使其工作。如果我從它上面切下來,它不會按預期工作。 – PunyViking
我不知道堆棧跟蹤是什麼,所以我不能發佈它...對不起! – PunyViking