python+QT designer 做图形化界面EXE程序

1、安装python  

2、安装QT designer 或QT creator 

3、打开QT designer 绘制你想要的图形化界面

类似这样

之后将文件保存,以ui后缀形式保存

4、下载安装pyside

直接easy_install 或pip安装 可能会保存,我安装的时候,提示 msvc 版本问题

查看许多国外论坛后,可以选择,pyside网站下载 对应Python版本的 whl安装包,下载成功之后,进入对应目录路径,直接 pip install  *.whl    *为包名

安装成功

5、将之前保存的ui后缀文件 转为py文件

pyside-uic  *.ui  -o  *.py

转换完成 就看到一个Python代码的 QT界面设计

# -*- coding: utf-8 -*-

# Form implementation generated from reading ui file 'test.ui'
#
# Created: Mon Apr 10 17:35:04 2017
# by: pyside-uic 0.2.15 running on PySide 1.2.4
#
# WARNING! All changes made in this file will be lost!

from PySide import QtCore, QtGui

class Ui_Frame(object):
def setupUi(self, Frame):
Frame.setObjectName("Frame")
Frame.resize(683, 596)
Frame.setFrameShape(QtGui.QFrame.StyledPanel)
Frame.setFrameShadow(QtGui.QFrame.Raised)
self.pushButton = QtGui.QPushButton(Frame)
self.pushButton.setGeometry(QtCore.QRect(270, 350, 75, 23))
self.pushButton.setObjectName("pushButton")
self.widget = QtGui.QWidget(Frame)
self.widget.setGeometry(QtCore.QRect(40, 390, 541, 171))
self.widget.setObjectName("widget")
self.frame = QtGui.QFrame(Frame)
self.frame.setGeometry(QtCore.QRect(30, 40, 621, 291))
self.frame.setFrameShape(QtGui.QFrame.StyledPanel)
self.frame.setFrameShadow(QtGui.QFrame.Raised)
self.frame.setObjectName("frame")
self.lineEdit = QtGui.QLineEdit(self.frame)
self.lineEdit.setGeometry(QtCore.QRect(20, 30, 31, 21))
self.lineEdit.setObjectName("lineEdit")
self.lineEdit_2 = QtGui.QLineEdit(self.frame)
self.lineEdit_2.setGeometry(QtCore.QRect(120, 30, 301, 21))
self.lineEdit_2.setObjectName("lineEdit_2")
self.commandLinkButton = QtGui.QCommandLinkButton(self.frame)
self.commandLinkButton.setGeometry(QtCore.QRect(520, 20, 91, 41))
self.commandLinkButton.setObjectName("commandLinkButton")
self.pushButton_2 = QtGui.QPushButton(self.frame)
self.pushButton_2.setGeometry(QtCore.QRect(430, 210, 75, 23))
self.pushButton_2.setObjectName("pushButton_2")
self.lineEdit_3 = QtGui.QLineEdit(self.frame)
self.lineEdit_3.setGeometry(QtCore.QRect(10, 70, 113, 20))
self.lineEdit_3.setObjectName("lineEdit_3")
self.lineEdit_4 = QtGui.QLineEdit(self.frame)
self.lineEdit_4.setGeometry(QtCore.QRect(10, 100, 113, 20))
self.lineEdit_4.setObjectName("lineEdit_4")
self.lineEdit_5 = QtGui.QLineEdit(self.frame)
self.lineEdit_5.setGeometry(QtCore.QRect(10, 130, 113, 20))
self.lineEdit_5.setObjectName("lineEdit_5")
self.lineEdit_6 = QtGui.QLineEdit(self.frame)
self.lineEdit_6.setGeometry(QtCore.QRect(10, 160, 113, 20))
self.lineEdit_6.setObjectName("lineEdit_6")
self.lineEdit_7 = QtGui.QLineEdit(self.frame)
self.lineEdit_7.setGeometry(QtCore.QRect(10, 190, 113, 20))
self.lineEdit_7.setObjectName("lineEdit_7")
self.lineEdit_8 = QtGui.QLineEdit(self.frame)
self.lineEdit_8.setGeometry(QtCore.QRect(140, 70, 113, 20))
self.lineEdit_8.setObjectName("lineEdit_8")
self.lineEdit_9 = QtGui.QLineEdit(self.frame)
self.lineEdit_9.setGeometry(QtCore.QRect(140, 100, 113, 20))
self.lineEdit_9.setObjectName("lineEdit_9")
self.lineEdit_10 = QtGui.QLineEdit(self.frame)
self.lineEdit_10.setGeometry(QtCore.QRect(140, 160, 113, 20))
self.lineEdit_10.setObjectName("lineEdit_10")
self.lineEdit_11 = QtGui.QLineEdit(self.frame)
self.lineEdit_11.setGeometry(QtCore.QRect(140, 130, 113, 20))
self.lineEdit_11.setObjectName("lineEdit_11")
self.lineEdit_12 = QtGui.QLineEdit(self.frame)
self.lineEdit_12.setGeometry(QtCore.QRect(140, 190, 113, 20))
self.lineEdit_12.setObjectName("lineEdit_12")
self.label_2 = QtGui.QLabel(self.frame)
self.label_2.setGeometry(QtCore.QRect(20, 10, 54, 12))
self.label_2.setObjectName("label_2")
self.comboBox = QtGui.QComboBox(self.frame)
self.comboBox.setGeometry(QtCore.QRect(60, 30, 51, 22))
self.comboBox.setObjectName("comboBox")
self.comboBox.addItem("")
self.comboBox.addItem("")
self.comboBox.addItem("")
self.comboBox.addItem("")
self.comboBox_2 = QtGui.QComboBox(self.frame)
self.comboBox_2.setGeometry(QtCore.QRect(450, 30, 51, 22))
self.comboBox_2.setObjectName("comboBox_2")
self.comboBox_2.addItem("")
self.comboBox_2.addItem("")
self.label = QtGui.QLabel(Frame)
self.label.setGeometry(QtCore.QRect(40, 10, 54, 12))
self.label.setObjectName("label")

self.retranslateUi(Frame)
QtCore.QMetaObject.connectSlotsByName(Frame)

def retranslateUi(self, Frame):
Frame.setWindowTitle(QtGui.QApplication.translate("Frame", "Frame", None, QtGui.QApplication.UnicodeUTF8))
self.pushButton.setText(QtGui.QApplication.translate("Frame", "start", None, QtGui.QApplication.UnicodeUTF8))
self.lineEdit.setText(QtGui.QApplication.translate("Frame", "URL", None, QtGui.QApplication.UnicodeUTF8))
self.lineEdit_2.setText(QtGui.QApplication.translate("Frame", "http://www.baidu.com", None, QtGui.QApplication.UnicodeUTF8))
self.commandLinkButton.setText(QtGui.QApplication.translate("Frame", "check", None, QtGui.QApplication.UnicodeUTF8))
self.pushButton_2.setText(QtGui.QApplication.translate("Frame", "选择文件", None, QtGui.QApplication.UnicodeUTF8))
self.label_2.setText(QtGui.QApplication.translate("Frame", "参数", None, QtGui.QApplication.UnicodeUTF8))
self.comboBox.setItemText(0, QtGui.QApplication.translate("Frame", "get", None, QtGui.QApplication.UnicodeUTF8))
self.comboBox.setItemText(1, QtGui.QApplication.translate("Frame", "post", None, QtGui.QApplication.UnicodeUTF8))
self.comboBox.setItemText(2, QtGui.QApplication.translate("Frame", "head", None, QtGui.QApplication.UnicodeUTF8))
self.comboBox.setItemText(3, QtGui.QApplication.translate("Frame", "delete", None, QtGui.QApplication.UnicodeUTF8))
self.comboBox_2.setItemText(0, QtGui.QApplication.translate("Frame", "true", None, QtGui.QApplication.UnicodeUTF8))
self.comboBox_2.setItemText(1, QtGui.QApplication.translate("Frame", "false", None, QtGui.QApplication.UnicodeUTF8))
self.label.setText(QtGui.QApplication.translate("Frame", "接口调试", None, QtGui.QApplication.UnicodeUTF8))

6、写程序给上述代码添加功能

# -*- coding:UTF-8 -*-
import sys,socket,requests,urllib,urllib2
import re,json
from urlparse import urlparse
# Import Qt GUI component
from PySide.QtGui import *

# Import GUI File
from http import Ui_Frame

class Frame(QFrame,Ui_Frame):

def __init__(self,parent=None):
super(Frame,self).__init__(parent)
self.setupUi(self)
self.pushButton.clicked.connect(self.cc)
self.commandLinkButton.clicked.connect(self.check)
self.pushButton_3.clicked.connect(self.clear)
self.pushButton_2.clicked.connect(self.clearargv)
def cc(self):
url=self.lineEdit_2.text()
method_txt=self.comboBox.currentText()
varify_txt=self.comboBox_2.currentText()
if varify_txt=='true':
varify=True
else:
varify=False
if method_txt=='get':
method=requests.get
elif method_txt=='post':
method=requests.post
elif method_txt=='head':
method=requests.head
else:
method=requests.delete
argv1=self.lineEdit_3.text()
argv2=self.lineEdit_8.text()
argv3 = self.lineEdit_4.text()
argv4 = self.lineEdit_9.text()
argv5 = self.lineEdit_5.text()
argv6 = self.lineEdit_10.text()
argv7 = self.lineEdit_6.text()
argv8 = self.lineEdit_11.text()
argv9 = self.lineEdit_7.text()
argv10 = self.lineEdit_12.text()
payload={
argv1:argv2,
argv3:argv4,
argv5: argv6,
argv7: argv8,
argv9: argv10,
}
try:
r=method(url,data=payload,verify=varify)
except:
print "something wrong"
t=str(r.text) + '\n' + str(r.headers)
self.lineEdit_13.setText(t)
print url,method_txt,r.text,payload,varify
def check(self):
a = self.lineEdit_2.text()
topHostPostfix = (
'.com', '.la', '.io', '.co', '.info', '.net', '.org', '.me', '.mobi',
'.us', '.biz', '.xxx', '.ca', '.co.jp', '.com.cn', '.net.cn',
'.org.cn', '.mx', '.tv', '.ws', '.ag', '.com.ag', '.net.ag',
'.org.ag', '.am', '.asia', '.at', '.be', '.com.br', '.net.br',
'.bz', '.com.bz', '.net.bz', '.cc', '.com.co', '.net.co',
'.nom.co', '.de', '.es', '.com.es', '.nom.es', '.org.es',
'.eu', '.fm', '.fr', '.gs', '.in', '.co.in', '.firm.in', '.gen.in',
'.ind.in', '.net.in', '.org.in', '.it', '.jobs', '.jp', '.ms',
'.com.mx', '.nl', '.nu', '.co.nz', '.net.nz', '.org.nz',
'.se', '.tc', '.tk', '.tw', '.com.tw', '.idv.tw', '.org.tw',
'.hk', '.co.uk', '.me.uk', '.org.uk', '.vg', ".com.hk")

regx = r'[^\.]+(' + '|'.join([h.replace('.', r'\.') for h in topHostPostfix]) + ')$'
pattern = re.compile(regx, re.IGNORECASE)

print "--" * 40
parts = urlparse(a)
host = parts.netloc
m = pattern.search(host)
res = m.group() if m else host
print "unkown" if not res else res
ip=socket.gethostbyname(host)
self.lineEdit_13.setText(ip)
print ip
def clear(self):
self.lineEdit_13.clear()
def clearargv(self):
self.lineEdit_3.clear()
self.lineEdit_4.clear()
self.lineEdit_5.clear()
self.lineEdit_6.clear()
self.lineEdit_7.clear()
self.lineEdit_8.clear()
self.lineEdit_9.clear()
self.lineEdit_10.clear()
self.lineEdit_11.clear()
self.lineEdit_12.clear()



if __name__=='__main__':
Program = QApplication(sys.argv)
Window=Frame()
Window.show()
Program.exec_()

7、将上述程序打包成EXE

使用pyinstaller

把第6步写的代码文件,复制到pyinstaller目录下

执行 Python pyinstaller.py -D -w *.py

——D的意思 生成目录,生成EXE以及一些依赖文件
-W 没有控制台

 


 

posted @ 2017-04-11 15:55  踏雪无痕何处是  阅读(6184)  评论(1编辑  收藏  举报