Kivy tutorial 008: More kv language

2024-06-22 20:04
文章标签 kivy tutorial 008 kv language

本文主要是介绍Kivy tutorial 008: More kv language,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

Kivy tutorial 008: More kv language – Kivy Blog

Central themes: Event binding and canvas instructions in kv language
中心主题: 事件绑定 和 kv语言里的画布结构

This tutorial directly follows on from the previous, so start by retrieving the previous code, as below:
这节导师课直接地跟随上节的,因此从上次的代码找了些代码开始。

main.py:

from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.slider import Sliderfrom kivy.uix.boxlayout import BoxLayout
from kivy.uix.label import Label
from kivy.uix.slider import Sliderfrom kivy.uix.widget import Widget
from kivy.graphics import Rectangle, Color, Linefrom random import randomclass DrawingWidget(Widget):def __init__(self):super(DrawingWidget, self).__init__()with self.canvas:Color(1, 1, 1, 1)self.rect = Rectangle(size=self.size,pos=self.pos)self.bind(pos=self.update_rectangle,size=self.update_rectangle)def update_rectangle(self, instance, value):self.rect.pos = self.posself.rect.size = self.sizedef on_touch_down(self, touch):super(DrawingWidget, self).on_touch_down(touch)if not self.collide_point(*touch.pos):returnwith self.canvas:Color(random(), random(), random())self.line = Line(points=[touch.pos[0], touch.pos[1]], width=2)def on_touch_move(self, touch):if not self.collide_point(*touch.pos):returnself.line.points = self.line.points + [touch.pos[0], touch.pos[1]]class Interface(BoxLayout):passclass DrawingApp(App):def build(self):root_widget = Interface()return root_widgetDrawingApp().run()

drawing.kv:

<Interface>:orientation: 'vertical'DrawingWidget:Slider:min: 0max: 1value: 0.5size_hint_y: Noneheight: 80Slider:min: 0max: 1value: 0.5size_hint_y: Noneheight: 80Slider:min: 0max: 1value: 0.5size_hint_y: Noneheight: 80BoxLayout:orientation: 'horizontal'size_hint_y: Noneheight: 80Label:text: 'output colour:'Widget:

The first thing to do is draw the coloured Rectangle that the final Widget uses to display an output colour, and for this we need to know how to draw canvas instructions in kv language. The syntax is as below:
第一件事 使 画多彩的矩形, 这矩形是最后的组件 用来

Widget:canvas:Color:rgb: 0, 1, 0  # using a fixed colour for nowRectangle:size: self.sizepos: self.pos

Run the code, and you’ll see another of kv language’s most important features; automatic event binding. In the original Python code of tutorial 7 we needed an extra .bind(...) call to make the be updated to always be placed within its Widget. In kv language this is not necessary, the dependency on self.size and self.pos is automatically detected, and a binding automatically created!

This is also the generic syntax for canvas instructions; first add canvas: (or canvas.before or canvas.after), then, indent by 4 spaces, and add canvas instructions much like you would Widgets. However, note that canvas instructions are not widgets.

The only thing now missing from the original Python interface implementation in tutorial 7 is having the Sliders automatically update the output colour rectangle. Change the <Interface>: rule to the following:

<Interface>:orientation: 'vertical'DrawingWidget:Slider:id: red_slidermin: 0max: 1value: 0.5size_hint_y: Noneheight: 80Slider:id: green_slidermin: 0max: 1value: 0.5size_hint_y: Noneheight: 80Slider:id: blue_slidermin: 0max: 1value: 0.5size_hint_y: Noneheight: 80BoxLayout:orientation: 'horizontal'size_hint_y: Noneheight: 80Label:text: 'output colour:'Widget:canvas:Color:rgb: red_slider.value, green_slider.value, blue_slider.valueRectangle:size: self.sizepos: self.pos

There are actually only two changes here; we gave each Slider an id declaration, and in the canvas Color referred to the sliders with this name. Giving a widget an id is just like naming it in Python so that you can refer to it elsewhere.

Thanks to kv’s automatic binding, this is all we need to do to have the Color update automatically whenever a slider value changes. Run the code, and you should see that things work exactly as they did in the original Python interface.

We can finish this tutorial with a couple of extra kv conveniences. First, just as we added an automatically updating Rectangle in the Widget kv, we can do the same for the background of the DrawingWidget. Delete the __init__ and update_rectangle methods in the Python DrawingWidget code, and add a new rule in the kv file:

<DrawingWidget>:canvas:Color:rgba: 1, 1, 1, 1Rectangle:pos: self.possize: self.size

Second, you might have noticed that there’s a lot of code duplication in each of the Slider rules - we set the same min, max, initial value`, ``size_hint_y` and height for every one. As is normal in Python, it would be natural to abstract this in a new class, so as to set each value only once. You can probably already see how to do this with what we’ve learned so far (make a new class YourSlider(Slider): in the Python and add a new <YourSlider>: rule in the kv), but I’ll note that you can even do this entirely in kv:

<ColourSlider@Slider>:min: 0max: 1value: 0.5size_hint_y: Noneheight: 80<Interface>:orientation: 'vertical'DrawingWidget:ColourSlider:id: red_sliderColourSlider:id: green_sliderColourSlider:id: blue_sliderBoxLayout:orientation: 'horizontal'size_hint_y: Noneheight: 80Label:text: 'output colour:'Widget:canvas:Color:rgb: red_slider.value, green_slider.value, blue_slider.valueRectangle:size: self.sizepos: self.pos

The new <ColourSlider@Slider>: rule defines a dynamic class, a Python class kv rule without a corresponding Python code definition. This is convenient if you want to do something repeatedly only in kv, and never access it from Python.

At this point, we’ve reached feature parity with the original Python code, and seen all the basics of kv language. In the next tutorial we’ll finish off the original purpose of all these sliders; letting the user set the colour of line that is drawn by the DrawingWidget.

Full code

main.py:

from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.slider import Sliderfrom kivy.uix.boxlayout import BoxLayout
from kivy.uix.label import Label
from kivy.uix.slider import Sliderfrom kivy.uix.widget import Widget
from kivy.graphics import Rectangle, Color, Linefrom random import randomclass DrawingWidget(Widget):def on_touch_down(self, touch):super(DrawingWidget, self).on_touch_down(touch)if not self.collide_point(*touch.pos):returnwith self.canvas:Color(random(), random(), random())self.line = Line(points=[touch.pos[0], touch.pos[1]], width=2)def on_touch_move(self, touch):if not self.collide_point(*touch.pos):returnself.line.points = self.line.points + [touch.pos[0], touch.pos[1]]class Interface(BoxLayout):passclass DrawingApp(App):def build(self):root_widget = Interface()return root_widgetDrawingApp().run()

drawing.kv:

<DrawingWidget>:canvas:Color:rgba: 1, 1, 1, 1Rectangle:pos: self.possize: self.size<ColourSlider@Slider>:min: 0max: 1value: 0.5size_hint_y: Noneheight: 80<Interface>:orientation: 'vertical'DrawingWidget:ColourSlider:id: red_sliderColourSlider:id: green_sliderColourSlider:id: blue_sliderBoxLayout:orientation: 'horizontal'size_hint_y: Noneheight: 80Label:text: 'output colour:'Widget:canvas:Color:rgb: red_slider.value, green_slider.value, blue_slider.valueRectangle:size: self.sizepos: self.pos

这篇关于Kivy tutorial 008: More kv language的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1085280

相关文章

shader language学习(1)——shader language简介背景

shader language,称为着色语言,shade在英语是阴影、颜色深浅的意思。shader language基于物体本身属性和光照条件,计算美格橡塑的颜色值。 实际上这种解释具有明显的时代局限性,在GPU编程发展的早期,shader language的提出目标是加强对图形处理算法的控制,所以对该语言的定义也针对于此。但随着技术的进步,目前的shader language早已经用于通用计算

Retrieval-Augmented Generation for Large Language Models A Survey

Retrieval-Augmented Generation for Large Language Models: A Survey 文献综述 文章目录 Retrieval-Augmented Generation for Large Language Models: A Survey 文献综述 Abstract背景介绍 RAG概述原始RAG先进RAG预检索过程后检索过程 模块化RAGMo

CesiumJS【Basic】- #008 通过canvas绘制billboard

文章目录 通过canvas绘制billboard1 目标2 实现 通过canvas绘制billboard 1 目标 通过canvas绘制billboard 2 实现 /** @Author: alan.lau* @Date: 2024-06-16 11:15:48* @LastEditTime: 2024-06-16 11:43:02* @LastEditors: al

Creating custom and compound Views in Android - Tutorial(翻译)

Creating custom and compound Views in Android - Tutorial(翻译) 译前的: 之前做了三篇学习笔记,从知乎上面看到了这篇英文的推荐,总的来说可以是一篇导读,没有相关的学习,看这篇,可以作为一个学习脉络导向;有相关的学习底子,可以作为一个基础夯实、思维理清。没想到一翻译就是四个多小时…英语渣,很多词句都不太准确,幸好有之前的学习基础打底…

OpenCV Tutorial roadmap

http://computer-vision-talks.com/opencv-tutorial-roadmap/          computer vision talks All you want and should know about computer vision is here Home  »  OpenCV Tutorial roadmap O

阅读笔记——《Large Language Model guided Protocol Fuzzing》

【参考文献】Meng R, Mirchev M, Böhme M, et al. Large language model guided protocol fuzzing[C]//Proceedings of the 31st Annual Network and Distributed System Security Symposium (NDSS). 2024.(CCF A类会议)【注】本

【windows|008】DNS服务详解

🍁博主简介: 🏅云计算领域优质创作者 🏅2022年CSDN新星计划python赛道第一名 🏅2022年CSDN原力计划优质作者 ​ 🏅阿里云ACE认证高级工程师 ​ 🏅阿里云开发者社区专家博主 💊交流社区:CSDN云计算交流社区欢迎您的加入! 目录 1、什么是DNS:域名系统 2、DNS服务中的关键概念 3、DNS的查询过程 4、按查询内容分类: 5、资源记录类型

Gobject tutorial 九

The GObject messaging system Closures closure是一个抽象、通用的概念,它包含一个函数和一些变量。作用就是实现函数回调功能。 我们看看GLib对closure是怎么定义的。 /*** GClosure:* @in_marshal: Indicates whether the closure is currently being invoked wi

算法008:四数之合

四数之和. - 备战技术面试?力扣提供海量技术面试资源,帮助你高效提升编程技能,轻松拿下世界 IT 名企 Dream Offer。https://leetcode.cn/problems/4sum/description/ 在前面的两个题中,我们已经完成了两数之和和三数之和,到本题四数之和,其实也只是前几个题的延伸,在三数之和的基础上再固定一个数字。 先固定一个数字,剩下三个数按照三数之和的方

PAT-L1-008. 求整数段和

L1-008. 求整数段和 时间限制 400 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 杨起帆 给定两个整数A和B,输出从A到B的所有整数以及这些数的和。 输入格式: 输入在一行中给出2个整数A和B,其中-100<=A<=B<=100,其间以空格分隔。 输出格式: 首先顺序输出从A到B的