1.tableWidget怎么控制大小,能否用label来替代?
2.如何添加动作,点击目录后导出内容?
3.通过setColumnStretch
设置的列宽,在目录导出后,约束失效了,这是为什么?
import sys
from PyQt5.QtWidgets import *
import urllib.request as request
import Spider
class MainScene(QWidget):
def __init__(self):
super().__init__()
self.contextGrid()
self.show()
def contextGrid(self):
self.resize(500,300);
self.grid = QGridLayout()
self.setLayout(self.grid)
self.novelNameEdit = QLineEdit("http://xs.dmzj.com/2012/index.shtml")
self.searchBtn = QPushButton("Search")
self.blankLabel = QLabel("")
self.novelText = QLabel("Content")
self.grid.addWidget(self.novelNameEdit,0,0,1,1)
self.grid.addWidget(self.searchBtn,0,1,1,1)
self.grid.addWidget(self.blankLabel,0,2,1,3)
self.grid.addWidget(self.novelText,1,1,1,4)
self.grid.setColumnStretch(1,1)
self.grid.setColumnStretch(2,1)
self.grid.setColumnStretch(3,3)
self.searchBtn.clicked.connect(self.searchBtnClick)
def searchBtnClick (self):
pass
Sp = Spider.Catalogue()
content = request.urlopen(self.novelNameEdit.text()).read()
content = str(content, 'utf-8')
Sp.feed(content)
Sp.close()
catalogueCount = len(Sp.catalogueList)
tableWidget = QTableWidget(catalogueCount,1)
tableWidget.horizontalHeader().setSectionResizeMode(QHeaderView.Stretch)
tableWidget.setEditTriggers(QAbstractItemView.NoEditTriggers)
for ca in range(catalogueCount):
tableWidget.setItem(ca,0,QTableWidgetItem(Sp.catalogueList[ca]))
self.grid.addWidget(tableWidget, 1, 0, 1, 1)
if __name__ == "__main__":
App = QApplication(sys.argv)
MS = MainScene()
sys.exit(App.exec_())
Spider.py
# -*- coding:utf-8 -*-
import html.parser as Pa
class Catalogue(Pa.HTMLParser):
a_t = False
alt = ""
title = ""
catalogueList = []
def handle_data(self, data):
if self.a_t is True:
self.catalogueList.append(data)
def handle_starttag(self, tag, attrs):
if str(tag).startswith("a"):
for key,value in attrs:
if key == "alt":
self.alt = value
elif key == "title":
self.title = value
elif key == "href" and (value.find("/2012/")) == 0 and (value.find("index.")) == -1 and (value.find(".txt")) == -1:
self.a_t = True
else:
self.a_t = False
def handle_endtag(self, tag):
if tag == "a":
self.a_t=False
class NovelText(Pa.HTMLParser):
a_t = False
def handle_starttag(self, tag, attrs):
if str(tag).startswith("p"):
for key,value in attrs:
if value == "novel_text":
self.a_t = True
break;
else:
self.a_t = False
def handle_data(self, data):
if self.a_t is True:
print(data)
怪我咯2017-04-18 09:05:56
1) 不能用label代替吧,毕竟不是一个东西
3) 见setColumnStretch说明
Sets the stretch factor of column column to stretch. The first column is number 0.
The stretch factor is relative to the other columns in this grid. Columns with a higher stretch factor take more of the available space.
The default stretch factor is 0. If the stretch factor is 0 and no other column in this table can grow at all, the column may still grow.
An alternative approach is to add spacing using addItem() with a QSpacerItem.
2) 感觉你tableWidget使用不规范,建议做成成员变量
self.tableWidget = None
然后contextGrid初始化,之后根据条数动态的往里面加数据。
import sys
from PyQt5.QtWidgets import *
from PyQt5.QtWidgets import *
import urllib.request as request
import Spider
class MyTableWidget(QTableWidget):
def __init__(self):
super().__init__()
self.setMinimumWidth(400)
self.setColumnCount(1)
self.setColumnWidth(0, 400)
self.setEditTriggers(QAbstractItemView.NoEditTriggers)
self.itemClicked.connect(self.handleItemClick)
def handleItemClick(self, item):
print(item.text())
class MainScene(QWidget):
def __init__(self, parent=None):
super().__init__()
self.grid = QGridLayout()
self.setLayout(self.grid)
self.novelNameEdit = QLineEdit("http://xs.dmzj.com/2012/index.shtml")
self.searchBtn = QPushButton("Search")
self.novelText = QLabel("Content")
self.tableWidget = MyTableWidget()
self.grid.addWidget(self.novelNameEdit, 0, 0, 1, 2)
self.grid.addWidget(self.searchBtn, 0, 2, 1, 1)
self.grid.addWidget(self.tableWidget, 1, 0, 2, 2)
self.grid.addWidget(self.novelText, 1, 2, 1, 1)
self.resize(1024, 600)
self.searchBtn.clicked.connect(self.searchBtnClick)
def searchBtnClick(self):
self.tableWidget.clear()
Sp = Spider.Catalogue()
content = request.urlopen(self.novelNameEdit.text()).read()
content = str(content, 'utf-8')
Sp.feed(content)
Sp.close()
catalogueCount = len(Sp.catalogueList)
self.tableWidget.setRowCount(catalogueCount)
for index, item in enumerate(Sp.catalogueList):
self.tableWidget.setItem(index, 0, QTableWidgetItem(item))
if __name__ == "__main__":
App = QApplication(sys.argv)
MS = MainScene()
MS.show()
sys.exit(App.exec_())