ukonline2000 发表于 2012-7-20 10:10:54

昨天写了一个简单的树莓派GPIO的图形控制程序(附源代码)!!!!

感谢大家一直以来的支持 ,所以趁昨天还有点时间,抽空写了一个简单的树莓派GPIO的图形控制程序,这样就方便那些觉得敲命令比较麻烦的童鞋, 分享给大家吧,希望对大家有所帮助!
http://player.youku.com/player.php/sid/XNDI5NjA1OTg4/v.swf

有了它,大家就可以简单的一开一关控制GPIO,甚至控制电器和小车一类的,当然最好是用扩展板
http://www.shumeipai.net/thread-466-1-1.html
废话不说了,先上图吧!

1.使用方法,装好GPIO的库,参考这两个文章

http://www.shumeipai.net/thread-406-1-1.html




2.下载源程序demo.py,在终端中用root登录(输入su -)


源程序


3.然后输入


python demo.py


记住需要在图形界面下



程序代码:
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

#!/usr/bin/env python
# -*- coding: utf-8 -*-
# generated by wxGlade 0.6.3 on Thu Jul 19 15:47:52 2012
# design by ukonline2000 form www.shumeipai.net
import wx

# begin wxGlade: extracode
import RPi.GPIO as GPIO
GPIO.setup(11,GPIO.OUT)
GPIO.setup(12,GPIO.OUT)
GPIO.setup(13,GPIO.OUT)
GPIO.setup(15,GPIO.OUT)
GPIO.setup(16,GPIO.OUT)
GPIO.setup(18,GPIO.OUT)
GPIO.setup(22,GPIO.OUT)
GPIO.setup(7,GPIO.OUT)
# end wxGlade



class MyFrame(wx.Frame):
    def __init__(self, *args, **kwds):
      # begin wxGlade: MyFrame.__init__
      kwds["style"] = wx.DEFAULT_FRAME_STYLE
      wx.Frame.__init__(self, *args, **kwds)
      self.label_2 = wx.StaticText(self, -1, "GPIO0")
      self.button_1 = wx.Button(self, -1, "ON")
      self.button_5 = wx.Button(self, -1, "OFF")
      self.label_3 = wx.StaticText(self, -1, "GPIO1")
      self.button_2 = wx.Button(self, -1, "ON")
      self.button_6 = wx.Button(self, -1, "OFF")
      self.label_4 = wx.StaticText(self, -1, "GPIO2")
      self.button_3 = wx.Button(self, -1, "ON")
      self.button_7 = wx.Button(self, -1, "OFF")
      self.label_5 = wx.StaticText(self, -1, "GPIO3")
      self.button_4 = wx.Button(self, -1, "ON")
      self.button_8 = wx.Button(self, -1, "OFF")      
      self.label_6 = wx.StaticText(self, -1, "GPIO4")
      self.button_9 = wx.Button(self, -1, "ON")
      self.button_10 = wx.Button(self, -1, "OFF")
      self.label_7 = wx.StaticText(self, -1, "GPIO5")
      self.button_11 = wx.Button(self, -1, "ON")
      self.button_12 = wx.Button(self, -1, "OFF")
      self.label_8 = wx.StaticText(self, -1, "GPIO6")
      self.button_13 = wx.Button(self, -1, "ON")
      self.button_14 = wx.Button(self, -1, "OFF")
      self.label_9 = wx.StaticText(self, -1, "GPIO7")
      self.button_15 = wx.Button(self, -1, "ON")
      self.button_16 = wx.Button(self, -1, "OFF")

      self.__set_properties()
      self.__do_layout()

      self.Bind(wx.EVT_BUTTON, self.on0, self.button_1)
      self.Bind(wx.EVT_BUTTON, self.off0, self.button_5)
      self.Bind(wx.EVT_BUTTON, self.on1, self.button_2)
      self.Bind(wx.EVT_BUTTON, self.off1, self.button_6)
      self.Bind(wx.EVT_BUTTON, self.on2, self.button_3)
      self.Bind(wx.EVT_BUTTON, self.off2, self.button_7)
      self.Bind(wx.EVT_BUTTON, self.on3, self.button_4)
      self.Bind(wx.EVT_BUTTON, self.off3, self.button_8)      
      self.Bind(wx.EVT_BUTTON, self.on4, self.button_9)
      self.Bind(wx.EVT_BUTTON, self.off4, self.button_10)
      self.Bind(wx.EVT_BUTTON, self.on5, self.button_11)
      self.Bind(wx.EVT_BUTTON, self.off5, self.button_12)
      self.Bind(wx.EVT_BUTTON, self.on6, self.button_13)
      self.Bind(wx.EVT_BUTTON, self.off6, self.button_14)
      self.Bind(wx.EVT_BUTTON, self.on7, self.button_15)
      self.Bind(wx.EVT_BUTTON, self.off7, self.button_16)
      # end wxGlade

    def __set_properties(self):
      # begin wxGlade: MyFrame.__set_properties
      self.SetTitle("Raspi GPIO Demo")
      self.SetSize((246, 330))
      self.label_2.SetMinSize((80, 40))
      self.label_2.SetFont(wx.Font(16, wx.DEFAULT, wx.NORMAL, wx.NORMAL, 0, ""))
      self.button_1.SetMinSize((80, 40))
      self.button_5.SetMinSize((80, 40))
      self.label_3.SetMinSize((80, 40))
      self.label_3.SetFont(wx.Font(16, wx.DEFAULT, wx.NORMAL, wx.NORMAL, 0, ""))
      self.button_2.SetMinSize((80, 40))
      self.button_6.SetMinSize((80, 40))
      self.label_4.SetMinSize((80, 40))
      self.label_4.SetFont(wx.Font(16, wx.DEFAULT, wx.NORMAL, wx.NORMAL, 0, ""))
      self.button_3.SetMinSize((80, 40))
      self.button_7.SetMinSize((80, 40))
      self.label_5.SetMinSize((80, 40))
      self.label_5.SetFont(wx.Font(16, wx.DEFAULT, wx.NORMAL, wx.NORMAL, 0, ""))
      self.button_4.SetMinSize((80, 40))
      self.button_8.SetMinSize((80, 40))
      self.label_6.SetMinSize((80, 40))
      self.label_6.SetFont(wx.Font(16, wx.DEFAULT, wx.NORMAL, wx.NORMAL, 0, ""))
      self.button_9.SetMinSize((80, 40))
      self.button_10.SetMinSize((80, 40))
      self.label_7.SetMinSize((80, 40))
      self.label_7.SetFont(wx.Font(16, wx.DEFAULT, wx.NORMAL, wx.NORMAL, 0, ""))
      self.button_11.SetMinSize((80, 40))
      self.button_12.SetMinSize((80, 40))
      self.label_8.SetMinSize((80, 40))
      self.label_8.SetFont(wx.Font(16, wx.DEFAULT, wx.NORMAL, wx.NORMAL, 0, ""))
      self.button_13.SetMinSize((80, 40))
      self.button_14.SetMinSize((80, 40))
      self.label_9.SetMinSize((80, 40))
      self.label_9.SetFont(wx.Font(16, wx.DEFAULT, wx.NORMAL, wx.NORMAL, 0, ""))
      self.button_15.SetMinSize((80, 40))
      self.button_16.SetMinSize((80, 40))
      # end wxGlade

    def __do_layout(self):
      # begin wxGlade: MyFrame.__do_layout
      sizer_1 = wx.BoxSizer(wx.VERTICAL)
      grid_sizer_1 = wx.GridSizer(8, 3, 0, 0)
      grid_sizer_1.Add(self.label_2, 0, 0, 0)
      grid_sizer_1.Add(self.button_1, 0, 0, 0)
      grid_sizer_1.Add(self.button_5, 0, 0, 0)
      grid_sizer_1.Add(self.label_3, 0, 0, 0)
      grid_sizer_1.Add(self.button_2, 0, 0, 0)
      grid_sizer_1.Add(self.button_6, 0, 0, 0)
      grid_sizer_1.Add(self.label_4, 0, 0, 0)
      grid_sizer_1.Add(self.button_3, 0, 0, 0)
      grid_sizer_1.Add(self.button_7, 0, 0, 0)
      grid_sizer_1.Add(self.label_5, 0, 0, 0)
      grid_sizer_1.Add(self.button_4, 0, 0, 0)
      grid_sizer_1.Add(self.button_8, 0, 0, 0)
      grid_sizer_1.Add(self.label_6, 0, 0, 0)
      grid_sizer_1.Add(self.button_9, 0, 0, 0)
      grid_sizer_1.Add(self.button_10, 0, 0, 0)
      grid_sizer_1.Add(self.label_7, 0, 0, 0)
      grid_sizer_1.Add(self.button_11, 0, 0, 0)
      grid_sizer_1.Add(self.button_12, 0, 0, 0)
      grid_sizer_1.Add(self.label_8, 0, 0, 0)
      grid_sizer_1.Add(self.button_13, 0, 0, 0)
      grid_sizer_1.Add(self.button_14, 0, 0, 0)
      grid_sizer_1.Add(self.label_9, 0, 0, 0)
      grid_sizer_1.Add(self.button_15, 0, 0, 0)
      grid_sizer_1.Add(self.button_16, 0, 0, 0)
      sizer_1.Add(grid_sizer_1, 1, wx.EXPAND, 0)
      self.SetSizer(sizer_1)
      self.Layout()
      # end wxGlade

    def on0(self, event): # wxGlade: MyFrame.<event_handler>
      print "GPIO 0 is ON!"
      GPIO.output(11,True)
      event.Skip()

    def off0(self, event): # wxGlade: MyFrame.<event_handler>
      print "GPIO 0 is OFF!"
      GPIO.output(11,False)
      event.Skip()


    def on1(self, event): # wxGlade: MyFrame.<event_handler>
      print "GPIO 1 is ON!"
      GPIO.output(12,True)
      event.Skip()

    def off1(self, event): # wxGlade: MyFrame.<event_handler>
      print "GPIO 1 is OFF!"
      GPIO.output(12,False)
      event.Skip()

    def on2(self, event): # wxGlade: MyFrame.<event_handler>
      print "GPIO 2 is ON!"
      GPIO.output(13,True)
      event.Skip()

    def off2(self, event): # wxGlade: MyFrame.<event_handler>
      print "GPIO 2 is OFF!"
      GPIO.output(13,False)
      event.Skip()

    def on3(self, event): # wxGlade: MyFrame.<event_handler>
      print "GPIO 3 is ON!"
      GPIO.output(15,True)
      event.Skip()

    def off3(self, event): # wxGlade: MyFrame.<event_handler>
      print "GPIO 3 is OFF!"
      GPIO.output(15,False)
      event.Skip()

    def on4(self, event):# wxGlade: MyFrame.<event_handler>
      print "GPIO 4 is ON!"
      GPIO.output(16,True)
      event.Skip()

    def off4(self, event):# wxGlade: MyFrame.<event_handler>
      print "GPIO 4 is OFF!"
      GPIO.output(16,False)
      event.Skip()

    def on5(self, event):# wxGlade: MyFrame.<event_handler>
      print "GPIO 5 is ON!"
      GPIO.output(18,True)
      event.Skip()

    def off5(self, event):# wxGlade: MyFrame.<event_handler>
      print "GPIO 5 is OFF!"
      GPIO.output(18,False)
      event.Skip()

    def on6(self, event):# wxGlade: MyFrame.<event_handler>
      print "GPIO 6 is ON!"
      GPIO.output(22,True)
      event.Skip()

    def off6(self, event):# wxGlade: MyFrame.<event_handler>
      print "GPIO 6 is OFF!"
      GPIO.output(22,False)
      event.Skip()

    def on7(self, event):# wxGlade: MyFrame.<event_handler>
      print "GPIO 7 is ON!"
      GPIO.output(7,True)
      event.Skip()

    def off7(self, event):# wxGlade: MyFrame.<event_handler>
      print "GPIO 7 is OFF!"
      GPIO.output(7,False)
      event.Skip()

# end of class MyFrame


if __name__ == "__main__":
    app = wx.PySimpleApp(0)
    wx.InitAllImageHandlers()
    frame_1 = MyFrame(None, -1, "")
    app.SetTopWindow(frame_1)
    frame_1.Show()
    app.MainLoop()

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

ukonline2000 发表于 2012-7-20 10:13:06

自己顶一个

menglongfc 发表于 2012-7-20 10:49:53

好敬业,我的还没开始玩呢

树老大 发表于 2012-7-20 10:52:12

U版绝对为国内树莓派作的繁荣出了贡献。。

ukonline2000 发表于 2012-7-20 10:53:53

树老小 发表于 2012-7-20 13:21:20

2K哥威武

树老小 发表于 2012-7-20 13:31:16

一定要用python才能控制么?C可以么?

focusheart 发表于 2012-7-20 14:58:17

不错啊~用的wx呢,赞美~

ukonline2000 发表于 2012-7-20 15:16:43

回 树老小 的帖子

树老小:一定要用python才能控制么?C可以么? (2012-07-20 13:31) images/back.gif

C应该是没问题的,没有python这么方便

glutamine 发表于 2012-7-21 00:43:27

u大被你抢先发了~~~~~话说用wx代码好长啊。我用pygtk刚把界面做好,手里没pi,没法测试
页: [1] 2 3
查看完整版本: 昨天写了一个简单的树莓派GPIO的图形控制程序(附源代码)!!!!