Pyside2 of Python makes a browser opening gadget

Fantasy Lab 2022-05-14 14:47:37 阅读数:93

pyside2pysidepythonmakesbrowser

This paper is biased towards the basis , Mainly familiar PySide2 Interface development , How the button calls the function ;

Finally, click the button , Open the URL function entered by the user ;

First, follow the address below , To make a ui Interface , And will ui The interface is converted to py Code :

python And PySide2 Button 、 Single line text box 、 Text label _ Fantasy lab blog -CSDN Blog python And gui Interface design , Here is PySide2 library , Contains text labels (label), Single line text box (lineEdit), Button (pushButton)https://blog.csdn.net/weixin_43775400/article/details/124763307 The main content of this article is in , Based on the above link ;

If you follow the tutorial linked above , Finally, you will get the following code :

# -*- coding: utf-8 -*-
################################################################################
## Form generated from reading UI file 'main.ui'
##
## Created by: Qt User Interface Compiler version 5.15.2
##
## WARNING! All changes made in this file will be lost when recompiling UI file!
################################################################################
from PySide2.QtCore import *
from PySide2.QtGui import *
from PySide2.QtWidgets import *
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
if not MainWindow.objectName():
MainWindow.setObjectName(u"MainWindow")
MainWindow.resize(508, 295)
self.centralwidget = QWidget(MainWindow)
self.centralwidget.setObjectName(u"centralwidget")
self.open_button = QPushButton(self.centralwidget)
self.open_button.setObjectName(u"open_button")
self.open_button.setGeometry(QRect(420, 30, 51, 21))
self.label = QLabel(self.centralwidget)
self.label.setObjectName(u"label")
self.label.setGeometry(QRect(50, 30, 31, 16))
self.lineEdit = QLineEdit(self.centralwidget)
self.lineEdit.setObjectName(u"lineEdit")
self.lineEdit.setGeometry(QRect(80, 30, 331, 20))
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QMenuBar(MainWindow)
self.menubar.setObjectName(u"menubar")
self.menubar.setGeometry(QRect(0, 0, 508, 23))
MainWindow.setMenuBar(self.menubar)
self.statusbar = QStatusBar(MainWindow)
self.statusbar.setObjectName(u"statusbar")
MainWindow.setStatusBar(self.statusbar)
self.retranslateUi(MainWindow)
QMetaObject.connectSlotsByName(MainWindow)
# setupUi
def retranslateUi(self, MainWindow):
MainWindow.setWindowTitle(QCoreApplication.translate("MainWindow", u"MainWindow", None))
self.open_button.setText(QCoreApplication.translate("MainWindow", u"\u6253\u5f00", None))
self.label.setText(QCoreApplication.translate("MainWindow", u"URL:", None))
self.lineEdit.setInputMask("")
# retranslateUi
if __name__ == "__main__":
import sys
app = QApplication(sys.argv)
MainWindow = QMainWindow()
ui = Ui_MainWindow()
ui.setupUi(MainWindow)
#==================
#==================
MainWindow.show()
sys.exit(app.exec_())

After running, you will get the following interface :

Because we need to realize that after the user clicks the open button , Open the URL entered by the user in the text box , So we need to put a... In the code webbrowser library , Otherwise, it will not be achieved , The user clicks the button to open the function of web address entered in advance ;

First, we write the function code to be called after clicking the button :

def url_go():
url = ui.lineEdit.text()
webbrowser.open(url)

The main purpose of this code , Get the content entered by the user in the text box , And assign values to variables url, And then use webbrowser.open(url) Open browser URL ;

And then we need to write , When the user clicks a button to call the code of the function ;

ui.open_button.clicked.connect(url_go)

  here , The required code is written , Let's take a look at the complete code :

# -*- coding: utf-8 -*-
################################################################################
## Form generated from reading UI file 'main.ui'
##
## Created by: Qt User Interface Compiler version 5.15.2
##
## WARNING! All changes made in this file will be lost when recompiling UI file!
################################################################################
from PySide2.QtCore import *
from PySide2.QtGui import *
from PySide2.QtWidgets import *
import webbrowser
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
if not MainWindow.objectName():
MainWindow.setObjectName(u"MainWindow")
MainWindow.resize(508, 295)
self.centralwidget = QWidget(MainWindow)
self.centralwidget.setObjectName(u"centralwidget")
self.open_button = QPushButton(self.centralwidget)
self.open_button.setObjectName(u"open_button")
self.open_button.setGeometry(QRect(420, 30, 51, 21))
self.label = QLabel(self.centralwidget)
self.label.setObjectName(u"label")
self.label.setGeometry(QRect(50, 30, 31, 16))
self.lineEdit = QLineEdit(self.centralwidget)
self.lineEdit.setObjectName(u"lineEdit")
self.lineEdit.setGeometry(QRect(80, 30, 331, 20))
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QMenuBar(MainWindow)
self.menubar.setObjectName(u"menubar")
self.menubar.setGeometry(QRect(0, 0, 508, 23))
MainWindow.setMenuBar(self.menubar)
self.statusbar = QStatusBar(MainWindow)
self.statusbar.setObjectName(u"statusbar")
MainWindow.setStatusBar(self.statusbar)
self.retranslateUi(MainWindow)
QMetaObject.connectSlotsByName(MainWindow)
# setupUi
def retranslateUi(self, MainWindow):
MainWindow.setWindowTitle(QCoreApplication.translate("MainWindow", u"MainWindow", None))
self.open_button.setText(QCoreApplication.translate("MainWindow", u"\u6253\u5f00", None))
self.label.setText(QCoreApplication.translate("MainWindow", u"URL:", None))
self.lineEdit.setInputMask("")
# retranslateUi
def url_go():
url = ui.lineEdit.text()
webbrowser.open(url)
if __name__ == "__main__":
import sys
app = QApplication(sys.argv)
MainWindow = QMainWindow()
ui = Ui_MainWindow()
ui.setupUi(MainWindow)
#==================
ui.open_button.clicked.connect(url_go)
#==================
MainWindow.show()
sys.exit(app.exec_())

Run this py file , Enter in the text box http://www.baidu.com Click the button... And try the effect ;

  If the user does not enter... In the text box , After clicking the open button , It will still perform the function of calling the browser , If the text box is empty , Is to Pop up alert , Users need to enter the small reminder function of the web address , It's mainly for familiarization QMessageBox function ;

So we just have to take url_go Function to change

def url_go():
url = ui.lineEdit.text()
if url=="" or url==" ":
QMessageBox.information(None, " Tips ", " Please enter URL")
else:
reply=QMessageBox.question(MainWindow,' Prompt information ', ' OK, click on ?', QMessageBox.Yes | QMessageBox.No, QMessageBox.Yes)
if reply==QMessageBox.Yes:
webbrowser.open(url)
else:
pass

More about python Language gui Interface development materials


If this article helps you , Please don't be stingy with your praise , thank you !

版权声明:本文为[Fantasy Lab]所创,转载请带上原文链接,感谢。 https://pythonmana.com/2022/134/202205141438472562.html