树莓派4
新推出的树莓派4性能卓越。运行桌面应用,比如浏览网页,看视频非常流畅。这一节我们在树莓派4上安装wxPython库,并编写一个桌面应用的例子。

raspberrypi_4.png
树莓派的桌面例子

2019-11-27-211852_1920x1080_scrot.png
树莓派系统没有预装wxPython的库,导入wxPython的库会报错
import wx
第一步,安装GTK3.0的库
sudo apt-get install libgtk-3-dev
第二步,安装wxPython库
刚开始使用下面的命令安装
sudo apt-get install python-wxgtk3.0
安装完成后,python2.7版本可以运行最简单的窗口应用。但Python3不行
最后使用下面的命令安装
sudo python3 -m pip install wxPython -i https://mirrors.aliyun.com/pypi/simple
安装成功。
但编译的过程花了很长时间(n小时以上。。。)
直接安装whl文件
将编译好的whl文件上传了。有需要的可以下载试试。
下载地址:
百度云盘链接 提取码: kfjn
运行以下命令,很快就可以安装好
sudo python3 -m pip install wxPython-4.0.7.post2-cp37-cp37m-linux_armv7l.whl
附测试的例子。
这个例子是在wxPython官网的例子基础上加了一个按钮和输入框
# coding: utf-8
import wx
# First things, first. Import the wxPython package.
class HelloFrame(wx.Frame):
"""
A Frame that says Hello World
"""
def __init__(self, *args, **kw):
# ensure the parent's __init__ is called
super(HelloFrame, self).__init__(*args, **kw)
# create a panel in the frame
pnl = wx.Panel(self)
# put some text with a larger bold font on it
st = wx.StaticText(pnl, label="Python示例")
font = st.GetFont()
font.PointSize += 10
font = font.Bold()
st.SetFont(font)
# and create a sizer to manage the layout of child widgets
sizer = wx.BoxSizer(wx.VERTICAL)
sizer.Add(st, wx.SizerFlags().Border(wx.TOP|wx.LEFT, 25))
pnl.SetSizer(sizer)
self.inputText = wx.TextCtrl(pnl,-1,"",pos=(80,100),size=(150,-1))
self.inputText.SetInsertionPoint(0) #设置焦点位置
openButton = wx.Button(pnl,label="Open",pos=(255,100),size=(80,25))
openButton.Bind(wx.EVT_BUTTON,self.openButtonClicked)
# create a menu bar
self.makeMenuBar()
# and a status bar
self.CreateStatusBar()
self.SetStatusText("Welcome to wxPython!")
def makeMenuBar(self):
"""
A menu bar is composed of menus, which are composed of menu items.
This method builds a set of menus and binds handlers to be called
when the menu item is selected.
"""
# Make a file menu with Hello and Exit items
fileMenu = wx.Menu()
# The "\t..." syntax defines an accelerator key that also triggers
# the same event
helloItem = fileMenu.Append(-1, "&Hello...\tCtrl-H",
"Help string shown in status bar for this menu item")
fileMenu.AppendSeparator()
# When using a stock ID we don't need to specify the menu item's
# label
exitItem = fileMenu.Append(wx.ID_EXIT)
# Now a help menu for the about item
helpMenu = wx.Menu()
aboutItem = helpMenu.Append(wx.ID_ABOUT)
# Make the menu bar and add the two menus to it. The '&' defines
# that the next letter is the "mnemonic" for the menu item. On the
# platforms that support it those letters are underlined and can be
# triggered from the keyboard.
menuBar = wx.MenuBar()
menuBar.Append(fileMenu, "&File")
menuBar.Append(helpMenu, "&Help")
# Give the menu bar to the frame
self.SetMenuBar(menuBar)
# Finally, associate a handler function with the EVT_MENU event for
# each of the menu items. That means that when that menu item is
# activated then the associated handler function will be called.
self.Bind(wx.EVT_MENU, self.OnHello, helloItem)
self.Bind(wx.EVT_MENU, self.OnExit, exitItem)
self.Bind(wx.EVT_MENU, self.OnAbout, aboutItem)
def OnExit(self, event):
"""Close the frame, terminating the application."""
self.Close(True)
def OnHello(self, event):
"""Say hello to the user."""
wx.MessageBox("Hello again from wxPython")
def OnAbout(self, event):
"""Display an About Dialog"""
wx.MessageBox("This is a wxPython Hello World sample",
"About Hello World 2",
wx.OK|wx.ICON_INFORMATION)
def openButtonClicked(self, event):
#"""Say hello to the user."""
wx.MessageBox("你输入了:"+self.inputText.GetValue())
#res = dialog.ShowModal()
print(self.inputText.GetValue())
if __name__ == '__main__':
# When this module is run (not imported) then create the app, the
# frame, show it, and start the event loop.
app = wx.App()
frm = HelloFrame(None, title='Hello World 2')
frm.Show()
app.MainLoop()