gpt4 book ai didi

python - Kivy Python 函数类和时钟的东西

转载 作者:太空宇宙 更新时间:2023-11-04 03:00:57 33 4
gpt4 key购买 nike

这主要是我没有掌握面向对象编程,而是涉及时钟函数

我希望我的 GUI 有一个启动持续更新功能的按钮(使用时钟功能安排)。并且还希望那个 GUI 有一个按钮来结束不断更新的功能。 (或者有更好的方法来完成这个吗?把它放在 if 语句中?当我按下那个按钮时改变的 bool 值,那个值会在 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()

所以当我尝试像这样运行它时,它会提示不知道什么是事件。令我印象深刻的是,在类函数之外声明它将使整个类都可以使用它。

建议?建议?感谢您的帮助

最佳答案

这是提示,因为您需要将一个变量定义为全局变量,以便在您尚未将其传入的函数中使用它,或使用 self.varname 将其附加到对象。此外,您正在创建事件,然后在调用构建之前立即取消它。

您应该有一个按钮,该按钮具有一个方法 on_release,该方法使用 Clock 启动一个 schedule_interval 函数调用。按下此按钮后应禁用。然后,您的第二个按钮将使用 cancel 取消计划。

这是一个基本的工作示例:

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()

和 .kv 文件:

<MyLayout>:
id: mylayoutid
orientation: 'vertical'
BoxLayout:
orientation: 'horizontal'
StartButton:
text: 'start'
id: button1
StopButton:
text: 'stop'
id: button2
Label:
id: mylabel

关于python - Kivy Python 函数类和时钟的东西,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40924286/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com