赞
踩
小编准备用PySide+QtDesiger开发个网络爬虫的界面,可是之前对Qt的界面布局不熟悉,经过几天的研究,也算小做成了一个,希望对读者有所帮助。
这个直接打开QtDesiger就可以了,拖拽控件,保存之后生成了一个main.ui文件
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>MainWindow</class>
<widget class="QMainWindow" name="MainWindow">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>1920</width>
<height>1440</height>
</rect>
</property>
<property name="minimumSize">
<size>
<width>1920</width>
<height>1440</height>
</size>
</property>
<property name="maximumSize">
<size>
<width>1920</width>
<height>1440</height>
</size>
</property>
<property name="windowTitle">
<string>新浪微博爬虫系统</string>
</property>
<widget class="QWidget" name="centralwidget">
<widget class="QTabWidget" name="tabWidget">
<property name="geometry">
<rect>
<x>40</x>
<y>40</y>
<width>1841</width>
<height>1361</height>
</rect>
</property>
<property name="minimumSize">
<size>
<width>1200</width>
<height>795</height>
</size>
</property>
<property name="maximumSize">
<size>
<width>1920</width>
<height>1440</height>
</size>
</property>
<property name="font">
<font>
<pointsize>12</pointsize>
</font>
</property>
<property name="toolTip">
<string><html><head/><body><p>数据分析</p></body></html></string>
</property>
<property name="currentIndex">
<number>0</number>
</property>
<widget class="QWidget" name="data_grap">
<property name="whatsThis">
<string><html><head/><body><p>数据爬取</p></body></html></string>
</property>
<attribute name="title">
<string>数据爬取</string>
</attribute>
<widget class="QWidget" name="">
<property name="geometry">
<rect>
<x>50</x>
<y>20</y>
<width>1751</width>
<height>1271</height>
</rect>
</property>
<layout class="QVBoxLayout" name="verticalLayout">
<item>
<layout class="QHBoxLayout" name="horizontalLayout">
<item>
<widget class="QLabel" name="keyLabel">
<property name="text">
<string>关键字</string>
</property>
</widget>
</item>
<item>
<widget class="QLineEdit" name="keyEdit"/>
</item>
<item>
<widget class="QPushButton" name="startBtn">
<property name="text">
<string>开始</string>
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="stopBtn">
<property name="text">
<string>停止</string>
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="refreshBtn">
<property name="text">
<string>刷新</string>
</property>
</widget>
</item>
</layout>
</item>
<item>
<layout class="QHBoxLayout" name="horizontalLayout_2">
<item>
<widget class="QLabel" name="label">
<property name="text">
<string>爬取进度</string>
</property>
</widget>
</item>
<item>
<widget class="QProgressBar" name="progressBar">
<property name="value">
<number>24</number>
</property>
</widget>
</item>
</layout>
</item>
<item>
<widget class="QTableView" name="sinaTable">
<attribute name="verticalHeaderShowSortIndicator" stdset="0">
<bool>true</bool>
</attribute>
</widget>
</item>
</layout>
</widget>
<zorder>keyLabel</zorder>
<zorder>keyEdit</zorder>
<zorder>startBtn</zorder>
<zorder>stopBtn</zorder>
<zorder>label</zorder>
<zorder>progressBar</zorder>
<zorder></zorder>
<zorder>tableView</zorder>
<zorder>sinaTable</zorder>
</widget>
<widget class="QWidget" name="data_analysis">
<property name="whatsThis">
<string><html><head/><body><p>数据分析</p></body></html></string>
</property>
<attribute name="title">
<string>数据分析</string>
</attribute>
</widget>
</widget>
</widget>
<widget class="QStatusBar" name="statusbar"/>
</widget>
<resources/>
<connections/>
</ui>
然后控制台到该文件文件的目录下。
c:/code/python>pyside-uic main.ui -o ui_main.py
就生成了main.py文件
如下
# -*- coding: utf-8 -*-
# Form implementation generated from reading ui file 'main.ui'
#
# Created: Tue May 02 16:16:03 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_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(1920, 1440)
MainWindow.setMinimumSize(QtCore.QSize(1920, 1440))
MainWindow.setMaximumSize(QtCore.QSize(1920, 1440))
self.centralwidget = QtGui.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.tabWidget = QtGui.QTabWidget(self.centralwidget)
self.tabWidget.setGeometry(QtCore.QRect(40, 40, 1841, 1361))
self.tabWidget.setMinimumSize(QtCore.QSize(1200, 795))
self.tabWidget.setMaximumSize(QtCore.QSize(1920, 1440))
font = QtGui.QFont()
font.setPointSize(12)
self.tabWidget.setFont(font)
self.tabWidget.setObjectName("tabWidget")
self.data_grap = QtGui.QWidget()
self.data_grap.setObjectName("data_grap")
self.widget = QtGui.QWidget(self.data_grap)
self.widget.setGeometry(QtCore.QRect(50, 20, 1751, 1271))
self.widget.setObjectName("widget")
self.verticalLayout = QtGui.QVBoxLayout(self.widget)
self.verticalLayout.setContentsMargins(0, 0, 0, 0)
self.verticalLayout.setObjectName("verticalLayout")
self.horizontalLayout = QtGui.QHBoxLayout()
self.horizontalLayout.setObjectName("horizontalLayout")
self.keyLabel = QtGui.QLabel(self.widget)
self.keyLabel.setObjectName("keyLabel")
self.horizontalLayout.addWidget(self.keyLabel)
self.keyEdit = QtGui.QLineEdit(self.widget)
self.keyEdit.setObjectName("keyEdit")
self.horizontalLayout.addWidget(self.keyEdit)
self.startBtn = QtGui.QPushButton(self.widget)
self.startBtn.setObjectName("startBtn")
self.horizontalLayout.addWidget(self.startBtn)
self.stopBtn = QtGui.QPushButton(self.widget)
self.stopBtn.setObjectName("stopBtn")
self.horizontalLayout.addWidget(self.stopBtn)
self.refreshBtn = QtGui.QPushButton(self.widget)
self.refreshBtn.setObjectName("refreshBtn")
self.horizontalLayout.addWidget(self.refreshBtn)
self.verticalLayout.addLayout(self.horizontalLayout)
self.horizontalLayout_2 = QtGui.QHBoxLayout()
self.horizontalLayout_2.setObjectName("horizontalLayout_2")
self.label = QtGui.QLabel(self.widget)
self.label.setObjectName("label")
self.horizontalLayout_2.addWidget(self.label)
self.progressBar = QtGui.QProgressBar(self.widget)
self.progressBar.setProperty("value", 0)
self.progressBar.setObjectName("grapProgress")
self.horizontalLayout_2.addWidget(self.progressBar)
self.verticalLayout.addLayout(self.horizontalLayout_2)
self.sinaTable = QtGui.QTableView(self.widget)
self.sinaTable.setObjectName("sinaTable")
self.sinaTable.verticalHeader().setSortIndicatorShown(True)
self.verticalLayout.addWidget(self.sinaTable)
self.tabWidget.addTab(self.data_grap, "")
self.data_analysis = QtGui.QWidget()
self.data_analysis.setObjectName("data_analysis")
self.tabWidget.addTab(self.data_analysis, "")
MainWindow.setCentralWidget(self.centralwidget)
self.statusbar = QtGui.QStatusBar(MainWindow)
self.statusbar.setObjectName("statusbar")
MainWindow.setStatusBar(self.statusbar)
self.retranslateUi(MainWindow)
self.tabWidget.setCurrentIndex(0)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
MainWindow.setWindowTitle(QtGui.QApplication.translate("MainWindow", "新浪微博爬虫系统", None, QtGui.QApplication.UnicodeUTF8))
self.tabWidget.setToolTip(QtGui.QApplication.translate("MainWindow", "<html><head/><body><p>数据分析</p></body></html>", None, QtGui.QApplication.UnicodeUTF8))
self.data_grap.setWhatsThis(QtGui.QApplication.translate("MainWindow", "<html><head/><body><p>数据爬取</p></body></html>", None, QtGui.QApplication.UnicodeUTF8))
self.keyLabel.setText(QtGui.QApplication.translate("MainWindow", "关键字", None, QtGui.QApplication.UnicodeUTF8))
self.startBtn.setText(QtGui.QApplication.translate("MainWindow", "开始", None, QtGui.QApplication.UnicodeUTF8))
self.stopBtn.setText(QtGui.QApplication.translate("MainWindow", "停止", None, QtGui.QApplication.UnicodeUTF8))
self.refreshBtn.setText(QtGui.QApplication.translate("MainWindow", "刷新", None, QtGui.QApplication.UnicodeUTF8))
self.label.setText(QtGui.QApplication.translate("MainWindow", "爬取进度", None, QtGui.QApplication.UnicodeUTF8))
self.tabWidget.setTabText(self.tabWidget.indexOf(self.data_grap), QtGui.QApplication.translate("MainWindow", "数据爬取", None, QtGui.QApplication.UnicodeUTF8))
self.data_analysis.setWhatsThis(QtGui.QApplication.translate("MainWindow", "<html><head/><body><p>数据分析</p></body></html>", None, QtGui.QApplication.UnicodeUTF8))
self.tabWidget.setTabText(self.tabWidget.indexOf(self.data_analysis), QtGui.QApplication.translate("MainWindow", "数据分析", None, QtGui.QApplication.UnicodeUTF8))
该文件要和上个ui_main.py文件放在同一个目录下
#!usr/bin/env python
#coding:utf-8
#这就是界面文件
import ui_main
import sys
from PySide.QtGui import QMainWindow,QApplication
from PySide.QtCore import Qt
class MainWindow(QMainWindow,ui_main.Ui_MainWindow):
def __init__(self, parent=None):
super(MainWindow,self).__init__(parent)
self.setupUi(self)
self.sinaTable.setGridStyle(Qt.PenStyle.SolidLine)
self.sinaTable.setColumnWidth(0,200)
self.sinaTable.setColumnWidth(2,300)
self.sinaTable.setColumnWidth(1,200)
self.sinaTable.setColumnWidth(3,1000)
self.startBtn.clicked.connect(self.__startBtn_Click)
self.refreshBtn.clicked.connect(self.__refreshBtn_Click)
def __startBtn_Click(self,row):
#开始按钮
pass
def __refreshBtn_Click(self):
#刷新数据
pass
到现在基本差不多了,就是调用这个类了,
import sys
#导入上面的类
import main
from PySide.QtGui import QApplication
if __name__ == '__main__':
program = QApplication(sys.argv)
app= main.MainWindow()
app.show()
program.exec_()
这样就完成了一个界面的开发,希望给读者一定帮助!
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。