【STM32MP135F-DK】6-使用GTK建立桌面应用程序
<div class='showpostmsg'><div>这次尝试使用python+gtk建立一个简单的桌面程序,去控制开发板上的LED。</div><div>首先使用vi在目录里建立一个hello.py的文件,代码如下:
<pre>
<code>import gi
gi.require_version("Gtk", "3.0")
from gi.repository import Gtk
win = Gtk.Window()
win.connect("destroy", Gtk.main_quit)
win.show_all()
Gtk.main()</code></pre>
<p> </p>
</div>
<div>使用 <em>python3 hello.py </em>运行这个python脚本,可以看到如下图的显示效果</div>
<div>
<div style="text-align: left;"></div>
<p> </p>
</div>
<div>下一步就是在界面上添加按键和按键的执行函数,代码如下
<pre>
<code>import gi
gi.require_version("Gtk", "3.0")
from gi.repository import Gtk
import gpiod
from gpiod.line import Direction, Value
class MyWindow(Gtk.Window):
def __init__(self):
super().__init__(title="CTRL LED")
self.button = Gtk.Button(label="LED")
self.button.connect("clicked", self.on_button_clicked)
self.add(self.button)
self.init_led()
self.led_state = 0
def init_led(self):
self.led_request = gpiod.request_lines(
"/dev/gpiochip9",
consumer="ctrl-led",
config={
14:gpiod.LineSettings(
direction=Direction.OUTPUT,output_value=Value.ACTIVE
)
},
)
def on_button_clicked(self, widget):
if self.led_state == 0:
self.led_state = 1
self.led_request.set_value(14,Value.ACTIVE)
else:
self.led_state = 0
self.led_request.set_value(14,Value.INACTIVE)
win = MyWindow()
win.connect("destroy", Gtk.main_quit)
win.show_all()
Gtk.main()</code></pre>
<p> </p>
</div>
<div>运行效果如下图所示,当点击按键的时候,下方的绿色LED点亮或者熄灭。</div>
<div>
<div></div>
<div> </div>
<p> </p>
</div>
</div><script> var loginstr = '<div class="locked">查看本帖全部内容,请<a href="javascript:;" style="color:#e60000" class="loginf">登录</a>或者<a href="https://bbs.eeworld.com.cn/member.php?mod=register_eeworld.php&action=wechat" style="color:#e60000" target="_blank">注册</a></div>';
if(parseInt(discuz_uid)==0){
(function($){
var postHeight = getTextHeight(400);
$(".showpostmsg").html($(".showpostmsg").html());
$(".showpostmsg").after(loginstr);
$(".showpostmsg").css({height:postHeight,overflow:"hidden"});
})(jQuery);
} </script><script type="text/javascript">(function(d,c){var a=d.createElement("script"),m=d.getElementsByTagName("script"),eewurl="//counter.eeworld.com.cn/pv/count/";a.src=eewurl+c;m.parentNode.insertBefore(a,m)})(document,523)</script>
页:
[1]