这主要是我没有掌握面向对象的编程,而是涉及时钟函数
我希望我的GUI有一个启动不断更新功能的按钮(使用时钟功能安排)。并且还希望GUI具有结束不断更新功能的按钮。 (或者有更好的方法来完成这个吗?把它放在一个if语句中?当我按下那个按钮时会改变它的布尔值,这个值会在python端更新吗?)
import kivy
import sqlite3
import sched, time
import smbus
import time
from Naked.toolshed.shell import execute_js, muterun_js
import os
import signal
import multiprocessing, signal
from kivy.uix.behaviors.button import ButtonBehavior
from kivy.uix.button import Button
from kivy.app import App
from kivy.uix.label import Label
from kivy.uix.widget import Widget
from kivy.base import runTouchApp
from kivy.clock import Clock
from kivy.properties import ListProperty
from kivy.properties import ObjectProperty
from kivy.vector import Vector
from kivy.core.window import Window
from kivy.lang import Builder
from kivy.uix.floatlayout import FloatLayout
from kivy.event import EventDispatcher
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.gridlayout import GridLayout
from kivy.uix.stacklayout import StackLayout
from kivy.core.image import Image
from kivy.uix.screenmanager import ScreenManager, Screen, FadeTransition, WipeTransition, SwapTransition
bus = smbus.SMBus(1)
address = 0x04
p = multiprocessing.Process(target = muterun_js,args=('iss_telemetry.js',)) #might delete this
conn = sqlite3.connect('iss_telemetry.db') #sqlite database call change to include directory
c = conn.cursor()
val = ""
def StringToBytes(val):
retVal = []
for c in val:
retVal.append(ord(c))
return retVal
class MainScreen(Screen):
def __init__(self, **kwargs):
super(MainScreen, self).__init__(**kwargs)
class CalibrateScreen(Screen):
pass
class ManualControlScreen(Screen):
def __init__(self, **kwargs):
super(ManualControlScreen, self).__init__(**kwargs)
def i2cWrite(self, *args):
bus.write_i2c_block_data(address, 0, StringToBytes(*args))
class MimicScreen(Screen, EventDispatcher):
def __init__(self, **kwargs):
super(MimicScreen, self).__init__(**kwargs)
class MainScreenManager(ScreenManager):
pass
class MyButton(Button):
pass
def point_inside_polygon(x, y, poly):
...
class TriangleButton(ButtonBehavior, Widget):
...
class MainApp(App):
event = Clock.schedule_interval(self.update_labels, 1)
event()
event.cancel()
def build(self):
self.mimic_screen = MimicScreen(name = 'mimic')
root = ScreenManager(transition=WipeTransition())
root.add_widget(MainScreen(name = 'main'))
root.add_widget(CalibrateScreen(name = 'calibrate'))
root.add_widget(self.mimic_screen)
root.add_widget(ManualControlScreen(name = 'manualcontrol'))
root.current= 'main'
# Clock.schedule_interval(self.update_labels, 1)
return root
def clockStart(self):
event()
def clockEnd(self):
event.cancel()
def i2cWrite(self, *args):
bus.write_i2c_block_data(address, 0, StringToBytes(*args))
def update_labels(self, dt):
c.execute('select two from telemetry')
values = c.fetchall()
psarj = values[0]
ssarj = values[1]
ptrrj = values[2]
strrj = values[3]
beta1b = values[4]
beta1a = values[5]
beta2b = values[6]
beta2a = values[7]
beta3b = values[8]
beta3a = values[9]
beta4b = values[10]
beta4a = values[11]
aos = values[12]
self.mimic_screen.ids.psarjvalue.text = str(psarj[0])[:-5]
self.mimic_screen.ids.ssarjvalue.text = str(ssarj[0])[:-5]
self.mimic_screen.ids.ptrrjvalue.text = str(ptrrj[0])[:-5]
self.mimic_screen.ids.strrjvalue.text = str(strrj[0])[:-5]
self.mimic_screen.ids.beta1bvalue.text = str(beta1b[0])[:-5]
self.mimic_screen.ids.beta1avalue.text = str(beta1a[0])[:-5]
self.mimic_screen.ids.beta2bvalue.text = str(beta2b[0])[:-5]
self.mimic_screen.ids.beta2avalue.text = str(beta2a[0])[:-5]
self.mimic_screen.ids.beta3bvalue.text = str(beta3b[0])[:-5]
self.mimic_screen.ids.beta3avalue.text = str(beta3a[0])[:-5]
self.mimic_screen.ids.beta4bvalue.text = str(beta4b[0])[:-5]
self.mimic_screen.ids.beta4avalue.text = str(beta4a[0])[:-5]
Builder.load_string('''
#:kivy 1.8
#:import kivy kivy
#:import win kivy.core.window
<MainScreen>:
...
<ManualControlScreen>:
...
<CalibrateScreen>:
...
<MimicScreen>:
name: 'mimic'
FloatLayout:
psarjvalue: psarjvalue
id: mimicscreenlayout
Image:
source: 'iss1.png'
allow_stretch: True
keep_ratio: False
...
Button:
id: mimicstartbutton
size_hint: 0.25,0.1
pos_hint: {"x": 0.07, "y": 0.6}
text: 'MIMIC'
disabled: False
font_size: 30
on_release: telemetrystatus.text = 'Sending Telemetry...'
on_release: app.clockStart()
on_release: mimicstopbutton.disabled = False
on_release: mimicstartbutton.disabled = True
Button:
id: mimicstopbutton
size_hint: 0.25,0.1
pos_hint: {"x": 0.07, "y": 0.4}
text: 'Stop'
disabled: True
font_size: 30
on_release: telemetrystatus.text = 'I2C Stopped'
on_release: app.clockStop()
on_release: mimicstopbutton.disabled = True
on_release: mimicstartbutton.disabled = False
Button:
size_hint: 0.3,0.1
pos_hint: {"Left": 1, "Bottom": 1}
text: 'Return'
font_size: 30
on_release: app.clockStop()
on_release: root.manager.current = 'main'
<TriangleButton>:
...
''')
if __name__ == '__main__':
MainApp().run()
以下是相关课程:
class MainApp(App):
event = Clock.schedule_interval(self.update_labels, 1)
event()
event.cancel()
def build(self):
self.mimic_screen = MimicScreen(name = 'mimic')
root = ScreenManager(transition=WipeTransition())
root.add_widget(MainScreen(name = 'main'))
root.add_widget(CalibrateScreen(name = 'calibrate'))
root.add_widget(self.mimic_screen)
root.add_widget(ManualControlScreen(name = 'manualcontrol'))
root.current= 'main'
# Clock.schedule_interval(self.update_labels, 1)
return root
def clockStart(root):
event()
def clockEnd(root):
event.cancel()
def i2cWrite(self, *args):
bus.write_i2c_block_data(address, 0, StringToBytes(*args))
def update_labels(self, dt):
...
这里是kv代码的相关部分(简化)
<MimicScreen>:
name: 'mimic'
FloatLayout:
...
Button:
...
on_release: app.clockStart()
Button:
...
on_release: app.clockStop()
Button:
...
on_release: app.clockStop()
因此,当我尝试像这样运行它时会抱怨不知道什么是事件。令人印象深刻的是,在类函数之外声明它会使它可供全班使用。
连连呢?建议吗?谢谢你的帮助
答案 0 :(得分:2)
It's complaining because you need to define a variable as global to use it inside a function you haven't passed it into or attach it to the object with self.varname
. Moreover, you're creating the event and then cancelling it right away before build has been called.
You should have a button that has a method on_release
that starts a schedule_interval
functional call with Clock
. This button should become disabled after being pressed. Your second button will then unschedule using cancel
.
Here's a basic working example:
from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.button import Button
from kivy.clock import Clock
class MyLayout(BoxLayout):
pass
class StartButton(Button):
def on_release(self):
self.event_handle = Clock.schedule_interval(self.clocks_are_fun,0.5)
self.disabled = True
def clocks_are_fun(self,dt):
self.parent.parent.ids.mylabel.text = 'Frames: ' + str(Clock.frames)
def closing_time(self):
self.event_handle.cancel()
class StopButton(Button):
def on_release(self):
self.parent.parent.ids.button1.closing_time()
self.parent.parent.ids.button1.disabled = False
class MyApp(App):
def build(self):
boxyFunTime = MyLayout()
return boxyFunTime
if __name__ == '__main__':
MyApp().run()
And the .kv file:
<MyLayout>:
id: mylayoutid
orientation: 'vertical'
BoxLayout:
orientation: 'horizontal'
StartButton:
text: 'start'
id: button1
StopButton:
text: 'stop'
id: button2
Label:
id: mylabel