相机校准前需要设置wifi的mac地址和切换校准模式,之前写的命令行工具,去了工厂发现使用可能有障碍,就做了个小应用程序,用了两种方法,先看一下第一种(不想选择的)
使用Tkinter做图形界面
Tkinter写界面麻烦,文档资料也不是很多,这里只做代码展示
#!/usr/bin/python# -*- coding: UTF-8 -*- from Tkinter import * # 导入 Tkinter 库from tkMessageBox import *import httplibimport re class MainWindow: def __init__(self): root = Tk() root.title("工具") width = 500 height = 300 screenwidth = root.winfo_screenheight() screenheight = root.winfo_screenheight() size = '%dx%d+%d+%d' % (width, height, (screenwidth - width) / 2, (screenheight - height) / 2) root.geometry(size) root.minsize(width, height) root.maxsize(width, height) Label(root, height=4, width=14, text="扫描数据:").grid(row=0, column=0, sticky=W) self.name = StringVar() Entry(root, width=30, textvariable=self.name).grid(row=0, column=1, columnspan=2) b1 = Button(root, text="发送MAC数据", height=4, width=10, command=self.send) b1.grid(row=2, column=0, sticky=E) b2 = Button(root, text="切换校准模式", height=4, width=10, command=self.close) b2.grid(row=2, column=1, sticky=E) b3 = Button(root, text="清空", height=4, width=10, command=self.clear) b3.grid(row=2, column=2, sticky=E) root.mainloop() def send(self): if self.name.get() == '': showwarning("输入为空", "参数不能为空,请重新操作!") else: result = re.sub(r"(?<=/w)(?=(?:/w/w)+$)", " ", self.name.get()) self.httpSend(result) def close(self): self.httpDisconnect() def clear(self): self.name.set('') # 创建HTTP连接 def httpSend(self, mac): httpClient = None # 进行操作 # 创建HTTP连接 def httpDisconnect(self): httpClient = None # 进行操作 if __name__ == '__main__': MainWindow()
使用PyQt4做图形界面
大学时候自学过一点Qt4.8.4的东西,所以比较倾向于用pyqt开发,跨平台,而且资料也比较齐全,特别是Qt脱离诺基亚后这几年发展迅猛,很多公司都开始采用Qt来开发图形界面应用了
首先配置环境
sudo apt-get install python-pip python2.7-dev libqt4-dev libqt4-dbg libqt4-gui libqt4-sql qt4-dev-tools qt4-doc qt4-designer qt4-qtconfig pyqt4-dev-tools
然后看代码
#!/usr/bin/python# -*- coding: utf-8 -*- import sysimport httplibimport refrom PyQt4.QtCore import *from PyQt4.QtGui import * #这里为了偷懒,用到啥导入啥 class MainWindow(QWidget): def __init__(self,parent=None): QWidget.__init__(self,parent) self.setWindowTitle(u'工具') mac = QLabel(u'扫描数据:') self.macEdit = QLineEdit() grid = QGridLayout() grid.addWidget(mac,1,0) grid.addWidget(self.macEdit,1,1) clear = QPushButton(u'清空') send = QPushButton(u'发送') disconnect = QPushButton(u'断开') grid.addWidget(clear,2,1) grid.addWidget(send,3,1) grid.addWidget(disconnect,4,1) self.setLayout(grid) self.resize(550,200) screen = QDesktopWidget().screenGeometry() size = self.geometry() self.move((screen.width()-size.width())/2,(screen.height()-size.height())/2) self.connect(clear, SIGNAL('clicked()'), self.clearAction) self.connect(send, SIGNAL('clicked()'), self.sendAction) self.connect(disconnect, SIGNAL('clicked()'), self.httpDisconnect) def clearAction(self): self.macEdit.clear() def sendAction(self): macText = self.macEdit.text() if macText == '': msgBox = QMessageBox(QMessageBox.Warning,u'输入为空',u'参数不能为空,请重新操作!') msgBox.exec_() else: result = re.sub(r"(?<=/w)(?=(?:/w/w)+$)", " ", unicode(macText)) # print(u'mac地址为:%s' % result) self.httpSend(result) # 创建HTTP连接 def httpSend(window,mac): httpClient = None try: response = httpClient.getresponse() if response.status == 200 and response.reason == 'OK': msgBox = QMessageBox(QMessageBox.Information,u'设置成功',u'相机MAC地址设置成功,请等待相机重新启动!') msgBox.exec_() except Exception, e: # print e msgBox = QMessageBox(QMessageBox.Warning,u'操作失败',u'操作失败,请重新操作!') msgBox.exec_() finally: if httpClient: httpClient.close() # 创建HTTP连接 def httpDisconnect(window): httpClient = None try: response = httpClient.getresponse() if response.status == 200 and response.reason == 'OK': msgBox = QMessageBox(QMessageBox.Information,u'断开成功',u'现在相机已经进入校准模式,可以进行校准!') msgBox.exec_() except Exception, e: # print e msgBox = QMessageBox(QMessageBox.Warning,u'操作失败',u'操作失败,请重新操作!') msgBox.exec_() finally: if httpClient: httpClient.close()#mainif __name__: app = QApplication(sys.argv) window = MainWindow() window.show() sys.exit(app.exec_())
新闻热点
疑难解答