第一步:装kivy库
在终端输入:
pip install kivy
第二步:复制代码
from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.slider import Slider
from kivy.uix.label import Label
from kivy.uix.togglebutton import ToggleButton
class AirConditionerApp(App):
def update_temp_label(self, instance, value):
# 温度值保留一位小数
self.temp_label.text = f"Current Temperature: {value:.1f}°C"
def toggle_ac(self, instance, state):
if state == 'down':
self.status_label.text = 'Status: ON'
else:
self.status_label.text = 'Status: OFF'
def build(self):
# 创建一个垂直的BoxLayout作为容器
layout = BoxLayout(orientation='vertical', padding=10, spacing=10)
# 创建一个标签显示空调状态
self.status_label = Label(text='Status: OFF', font_size='20sp')
# 创建一个Toggle按钮用来表示空调的开/关状态
ac_toggle = ToggleButton(text='Power', size_hint=(1, 0.1))
ac_toggle.bind(state=self.toggle_ac)
# 创建一个Slider对象用来调节温度
self.temp_slider = Slider(min=16, max=30, value=24)
self.temp_slider.bind(value=self.update_temp_label)
# 创建一个Label对象用来显示当前温度
self.temp_label = Label(text=f"Current Temperature: {self.temp_slider.value}°C", font_size='20sp')
# 将组件添加到BoxLayout
layout.add_widget(self.status_label)
layout.add_widget(self.temp_slider)
layout.add_widget(self.temp_label)
layout.add_widget(ac_toggle)
return layout
# 运行应用
if __name__ == '__main__':
AirConditionerApp().run()
第三步:运行结果
这个简单的空调界面允许用户通过滑动条调节温度,并通过切换按钮来开启或关闭空调。温度的范围是16°C到30°C,这是一个常见的室内温度范围。代码中的温度值在update_temp_label
函数中保留了一位小数,你也可以根据需要调整这一设置。
第四步:优化完善
我们可以对程序进行功能上的完善与优化,增加选择空调的工作模式(制冷、制热、自动、通风),设置风速(低、中、高),并且使用一个滑块来设定定时器(从0到240分钟)。每次点击相应的模式或风速按钮时,空调的模式或风速状态将会更新。滑动定时器滑块时,定时器的状态也会随之更新。得到的效果如下:
完整代码如下:
from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.slider import Slider
from kivy.uix.label import Label
from kivy.uix.togglebutton import ToggleButton
from kivy.uix.button import Button
class AirConditionerApp(App):
def update_temp_label(self, instance, value):
self.temp_label.text = f"Current Temperature: {value:.0f}°C"
def toggle_ac(self, instance, state):
if state == 'down':
self.status_label.text = 'Status: ON'
else:
self.status_label.text = 'Status: OFF'
def change_mode(self, instance):
self.mode_label.text = f"Mode: {instance.text}"
def adjust_fan_speed(self, instance):
self.fan_speed_label.text = f"Fan Speed: {instance.text}"
def set_timer(self, instance):
self.timer_label.text = f"Timer: {self.timer_slider.value:.0f} min"
def build(self):
# 创建一个垂直的BoxLayout作为容器
layout = BoxLayout(orientation='vertical', padding=10, spacing=10)
# 创建空调状态标签
self.status_label = Label(text='Status: OFF', font_size='20sp')
# 创建温度滑块和标签
self.temp_slider = Slider(min=16, max=30, value=24)
self.temp_slider.bind(value=self.update_temp_label)
self.temp_label = Label(text=f"Current Temperature: {self.temp_slider.value}°C", font_size='20sp')
# 创建模式选择按钮
self.mode_label = Label(text='Mode: Auto', font_size='20sp')
modes = BoxLayout(size_hint_y=None, height=50)
for mode in ['Cool', 'Heat', 'Auto', 'Fan']:
btn = Button(text=mode)
btn.bind(on_release=self.change_mode)
modes.add_widget(btn)
# 创建风速选择按钮
self.fan_speed_label = Label(text='Fan Speed: Medium', font_size='20sp')
fan_speeds = BoxLayout(size_hint_y=None, height=50)
for speed in ['Low', 'Medium', 'High']:
btn = Button(text=speed)
btn.bind(on_release=self.adjust_fan_speed)
fan_speeds.add_widget(btn)
# 创建定时器滑块和标签
self.timer_slider = Slider(min=0, max=240, value=0)
self.timer_slider.bind(value=self.set_timer)
self.timer_label = Label(text='Timer: Off', font_size='20sp')
# 创建空调开关按钮
ac_toggle = ToggleButton(text='Power', size_hint=(1, 0.1))
ac_toggle.bind(state=self.toggle_ac)
# 将组件添加到BoxLayout
layout.add_widget(self.status_label)
layout.add_widget(self.temp_label)
layout.add_widget(self.temp_slider)
layout.add_widget(self.mode_label)
layout.add_widget(modes)
layout.add_widget(self.fan_speed_label)
layout.add_widget(fan_speeds)
layout.add_widget(self.timer_label)
layout.add_widget(self.timer_slider)
layout.add_widget(ac_toggle)
return layout
# 运行应用
if __name__ == '__main__':
AirConditionerApp().run()