想要点击一个按键,运行一个程序,是很自然的想法,如何去实现呢?
写一个小程序:
import sys
from PyQt5.QtWidgets import QApplication, QWidget
from PyQt5.QtGui import QIcon,QFont
from PyQt5.QtWidgets import QWidget, QToolTip, QPushButton, QMessageBox, QApplication
from PyQt5.QtCore import QCoreApplication
class Example(QWidget):
def __init__(self):
super().__init__()
self.initUI()
def initUI(self):
def helloworld():
print('hello, world')
##创建窗口:
self.setGeometry(300, 300, 300, 220)
self.setWindowTitle('FirstPyQtProgram')
##创建按钮:
qb = QPushButton('Button', self)##创建一个按钮
qb.resize(qb.sizeHint())
qb.move(50, 50)
##链接按钮行为:
qb.clicked.connect(helloworld)
##创建按钮提示框:
qb.setToolTip('This is a <b>HelloWorld</b> Button')
self.show()
if __name__ == '__main__':
app = QApplication(sys.argv)
ex = Example()
sys.exit(app.exec_())
这样子就实现了一个功能:点击按钮,就会调用helloworld函数。以此为基础,其实已经可以实现自己写的函数与pyqt图形界面的对接了。什么信号与槽,以后再说,我先用用总没事吧。
将自己一个函数做个简单的ui:
getdata.py:
import urllib.request
from bs4 import BeautifulSoup
import os
def getdata(url):
response = urllib.request.urlopen(url)
html = response.read()
html = html.decode("gb2312")
soup = BeautifulSoup(html, 'html.parser')
book = soup.prettify()
data = book.split('\n')
num = len(data)
i = 0
ii = 1
s = []
ss = []
while i < num:
if data[i] == ' GPS控制点WGS84坐标':
while ii == 1:
if data[i] == ' 采集数据':
ii = 0
else:
if data[i] != ' 采集数据':
s.append(data[i])
i += 1
else:
i += 1
i = 0
num = len(s)
while i < num:
ii = 0
n = len(s[i])
while ii < n:
if s[i][ii] != '<':
flag = 0
ii += 1
else:
flag = 1
ii = n
if flag == 0:
ss.append(s[i].strip())
i += 1
else:
i += 1
d = [ss[11]+'\n',
ss[13].replace('.','#')+' '+ss[14].replace('.','#')+' '+ss[15]+'\n',
ss[20].replace('.','#')+' '+ss[21].replace('.','#')+' '+ss[22]+'\n',
ss[24].replace('.','#')+' '+ss[25].replace('.','#')+' '+ss[26]+'\n',
ss[28].replace('.','#')+' '+ss[29].replace('.','#')+' '+ss[30]+'\n',
ss[16].replace('.','#')+' '+ss[17].replace('.','#')+' '+ss[18]+'\n']
i = 0
num = len(d)
while i < num:
d[i] = d[i].replace('°','.')
d[i] = d[i].replace('′','')
d[i] = d[i].replace('″','')
d[i] = d[i].replace('#','')
d[i] = d[i].replace(':','.',1)
d[i] = d[i].replace(':','',1)
d[i] = d[i].replace(':','.',1)
d[i] = d[i].replace(':','',1)
i += 1
f_write = open('WGS84坐标.txt','a')
for i in d:
f_write.write(i)
f_write.close()
readhtml.py:
import sys
from PyQt5.QtWidgets import QApplication, QWidget
from PyQt5.QtGui import QIcon,QFont
from PyQt5.QtWidgets import QWidget, QToolTip, QPushButton, QMessageBox, QApplication
from PyQt5.QtCore import QCoreApplication
import os
import getdata
class Example(QWidget):
def __init__(self):
super().__init__()
self.initUI()
def initUI(self):
def readhtml():
url ='file:///C:/Users/zk/Desktop/readhtml/html-%E6%80%9D%E6%8B%93%E5%8A%9B/E15_071613.html'
getdata.getdata(url)
print('完成')
##创建窗口:
self.setGeometry(300, 300, 300, 220)
self.setWindowTitle('FirstPyQtProgram')
##创建按钮:
qb = QPushButton('ReadHtml', self)##创建一个按钮
qb.resize(qb.sizeHint())
qb.move(50, 50)
##链接按钮行为:
qb.clicked.connect(readhtml)
self.show()
if __name__ == '__main__':
app = QApplication(sys.argv)
ex = Example()
sys.exit(app.exec_())
这样就简单地将函数与pyqt结合起来了。缺点还是有的:
第一,若是可以实现点击按键,弹出选择文件或文件夹目录,就比较好;
第二,窗口内应该添加文字提示;
这两个要求应该还是比较容易实现的。好好努力,改进这个程序。
网友评论