自己修改了代码:实现了自动生成SSN,手机号和姓名的功能
import sys from PyQt5.QtGui import * from PyQt5.QtWidgets import * from PyQt5.QtCore import * import requests from generator import random_ssn,random_phone_number,random_name class MyTest(QWidget): def __init__(self, parent=None): super(MyTest, self).__init__(parent) self.label = QLabel(self) # self.label.setText("生成SSN") # self.lineEdit = QLineEdit() #self.lineEdit.setText("http://www.baidu.com") self.button_ssn = self.models("SSN",self.random_ssn) self.button_pnumber = self.models("手机号",self.random_number) self.button_name = self.models("姓名",self.random_name) self.text = QTextEdit() #信号于槽 #布局嵌套 wlayout = QVBoxLayout(self) #全局布局 hlayout = QHBoxLayout() #局部布局 vlayout = QVBoxLayout() #局部布局 hlayout.addWidget(self.label) # hlayout.addWidget(self.lineEdit) hlayout.addWidget(self.button_ssn,stretch=10) hlayout.addWidget(self.button_pnumber) hlayout.addWidget(self.button_name) vlayout.addWidget(self.text) wlayout.addLayout(hlayout) #将局部布局加到全局布局中 wlayout.addLayout(vlayout) #添加标题 self.setWindowTitle("生成SSN小工具") #添加图标 self.setWindowIcon(QIcon(r'F:GitHubBuy123061.png')) def models(self,name,func): self.button = QPushButton(name) self.button.clicked.connect(func) return self.button #槽函数 def random_ssn(self): # url = self.lineEdit.text() # rep = requests.get(url) # rep.encoding = 'utf-8' # html = rep.text for i in range(5): ssn = random_ssn() #将抓取的网页源码加入到textEdit中 #setText()这个函数不能实现 self.text.setPlainText(ssn) def random_number(self): phoneNumber = random_phone_number() self.text.setPlainText(phoneNumber) def random_name(self): name = random_name() self.text.setPlainText(name) if __name__ =="__main__": app = QApplication(sys.argv) demo = MyTest() demo.show() sys.exit(app.exec())
网上copy他人代码,这里借鉴一下:
import sys from PyQt5.QtGui import * from PyQt5.QtWidgets import * from PyQt5.QtCore import * import requests class MyTest(QWidget): def __init__(self, parent=None): super(MyTest, self).__init__(parent) self.label = QLabel(self) self.label.setText("网址") self.lineEdit = QLineEdit() #self.lineEdit.setText("http://www.baidu.com") self.button = QPushButton("开始") self.text = QTextEdit() #信号于槽 self.button.clicked.connect(self.getstr) #布局嵌套 wlayout = QVBoxLayout(self) #全局布局 hlayout = QHBoxLayout() #局部布局 vlayout = QVBoxLayout() #局部布局 hlayout.addWidget(self.label) hlayout.addWidget(self.lineEdit) hlayout.addWidget(self.button) vlayout.addWidget(self.text) wlayout.addLayout(hlayout) #将局部布局加到全局布局中 wlayout.addLayout(vlayout) #添加标题 self.setWindowTitle("URL抓取") #添加图标 self.setWindowIcon(QIcon('1.ico')) #槽函数 def getstr(self): url = self.lineEdit.text() rep = requests.get(url) rep.encoding = 'utf-8' html = rep.text #将抓取的网页源码加入到textEdit中 #setText()这个函数不能实现 self.text.setPlainText(html) if __name__ =="__main__": app = QApplication(sys.argv) demo = MyTest() demo.show() sys.exit(app.exec())