Reputation: 5089
I'm not really sure how to ask this question so it makes the most sense. I have a console-based Python program that simulates a fuel storage and transfer system (schematic drawing).
The current program is nothing special, just uses valve and pump instances to see if valves are open/closed, pumps on/off, and the associated flow rates and pressure across the devices.
I'm looking to make a Kivy program that simulates a human-machine interface. In this case, simple toggle buttons next to the valves to show if they are open or closed, and whether the pumps are on or off.
I've gone through the Kivy tutorials and read a Kivy book but they don't answer my problems, as they tend to focus more on mobile games rather than desktop-oriented apps. I've figured out how to add the buttons to the schematic but I don't know how to make the .py/.kv files interact with my existing Python code. I don't want to rewrite my existing code if at all possible.
Below is what I have so far; all it does is make a ToggleButton underneath valve 1. When pressed, the button sends the word "pressed" to the console output.
hmi.py
import kivy
kivy.require("1.10.0")
from kivy.app import App
from kivy.uix.floatlayout import FloatLayout
from kivy.config import Config
from kivy.uix.togglebutton import ToggleButton, ToggleButtonBehavior
Config.set("graphics", "width", "1062")
Config.set("graphics", "height", "849")
class HMI(FloatLayout):
pass
class HMIApp(App):
def build(self):
return HMI()
if __name__ == "__main__":
HMIApp().run()
hmi.kv
#:kivy 1.10.0
<HMIButton@ToggleButton>:
color: 1, 1, 1, 1
size_hint: (.01, .01)
<HMI>:
canvas.before:
Rectangle:
pos: self.pos
size: self.size
source: "fuel_schematic.png"
HMIButton:
id: _valve_1
text: "1"
pos: 360, 285
on_press: print("pressed")
My main question is, what is the best way to take the valve instance at VirtualPLC.Models.FuelFarm.components.gate1 and the functions at VirtualPLC.Models.FuelFarm.functionality.gate1_open() [and close()] and tie them to the toggle button I've added to the Kivy code?
Can I simply import these modules or do I have to rewrite them into the Kivy program?
Upvotes: 0
Views: 526
Reputation: 16011
When you touch or click on ToggleButton widget, the state toggles between ‘normal’
and ‘down’
(as opposed to a Button that is only ‘down’
as long as it is pressed).
Yes, you can import those modules.
on_press
with on_state
event, and bind it to a callback method and passing an instance of the ToggleButton e.g. on_state: root.on_state(self)
.group:
to each ToggleButton so that you know which value it is.HMIButton:
id: _valve_1
text: "1"
pos: 360, 285
group: "valve1"
on_state: root.on_state(self)
Implement a callback method inside class HMI().
class HMI(FloatLayout):
def on_state(self, instance):
if instance.state == "down":
print(instance.group, "Opened")
else:
print(instance.group, "Closed")
Upvotes: 1