Pyqt5 GUI & & requests API makes a weather query system (get the complete code at the end of the text)

Time:2022-5-10

Before understanding, let’s check the effect of the completed desktop application. In fact, the function is relatively simple. It is to pyqt5 make a desktop application and request to be associated with the open source weather query API through the requests network, so as to complete the whole application. Let’s learn about it through a simple video!

file

Full text reading

Write a function init_ UI () is used to lay out the UI interface of the application:

'' UI interface processing function '' '
    def init_ui(self):
        self. Setwindowtitle ('urban weather query system ')

        Grid = qgridlayout() # initialize grid layout

        self. solr_ line_ Text = qlineedit() # initializes the search content input box
        self. solr_ line_ text. Setplaceholdertext ('enter city name ') # sets the default content
        self. solr_ line_ text. Setfocus() # mouse focus

        self. query_ Button = QPushButton ('query ') # initialize query button
        self.query_button.clicked.connect(lambda: self.query_button_click(self.solr_line_text))

        self. res_ Text = qtextedit() # initializes the text input box
        self. res_ text. Setplaceholdertext ('query result display area... '# initializes the prompt content
        self. res_ text. Setreadonly (true) # sets the read-only mode

        '' add content to layout '' '
        grid.addWidget(self.solr_line_text, 0, 0)
        grid.addWidget(self.query_button, 0, 1)
        grid.addWidget(self.res_text, 1, 0, 1, 2)

        self.setLayout(grid)

Write a slot function and send the query button signal to the slot function to bind the button to the function.

def query_button_click(self,solr_line_text):
        '''
        Weather query button binding function (slot function)
        :param solr_line_text:
        :return:
        '''
        if solr_line_text.text():
            text = solr_line_text.text()
            self.request_init(text)

        else:
            warn_dialog = QDialog()
            warn_ dialog. Setwindowtitle ('warning ')
            warn_dialog.resize(80,40)

            warn_label_text = QLabel(warn_dialog)
            warn_ label_ text. Settext ('Please enter city name! ')
            warn_label_text.move(7,15)

            warn_dialog.setWindowModality(Qt.ApplicationModal)
            warn_dialog.exec()

Next, send the network request through the requests module.

file

Hidden benefits: reply to the “weather query system” in the official account to obtain the complete code.

file

A beautiful windows CMD command line tool cmder

How to write and save visual data after Excel data analysis!

Excel data processing II: quickly complete the addition and modification of openpyxl data!

Excel data processing 1: skillfully use openpyxl to extract and filter data

Helium, a more convenient automated testing tool than selenium!

Recommended Today

React18 is officially released. What is the future development trend?

Hello, I’m Carson. March 29, 2022,React18Official release. fromv16Start,ReactThe team is becoming popularConcurrentThe concept of. stayv18In the iterative process of (alpha, beta, RC), it has also been popular scienceConcurrency characteristicsTherefore, when the official version was released, there were no new features. This article mainly explainsv18Some future trends revealed in the release log. Welcome to join the […]