事件的定义

  • 设计窗体的继承

设计了UI后,用pynic5,或者在Eric6中选择编译窗体,生成.py文件,然后在主窗体中import导入。

from Ui_qwMainWindow import Ui_MainWindow

class MainWindow(QtWidgets.QMainWindow, Ui_MainWindow):
    def __init__(self, parent=None):
        super(MainWindow, self).__init__(parent)
        self.setupUi(self)

之后在类的初始化 init 方法中定义自己的属性和事件

  • 输入框的回车捕获,以及查询按钮的点击

用户输入城市名称后,按回车键就开始进行查询,所以需要在输入框中捕获回车的输入。

self.inputLineEdit.returnPressed.connect(self.query_action)

将returnPressed事件与自己定义的quert_action方法绑定,输入框捕捉到回车时,调用query_action方法。

  • API ComboBox菜单的选择

用户更换API时,选择不同的下拉菜单项后,触发事件。可以通过ComboBox的index值变化进行捕捉:

self.apiComboBox.currentIndexChanged['int'].connect(self.api_change)

我这里定义是下拉菜单项变化时,执行自定义的api_change方法。

  • 温度单位ComboBox菜单的选择

    self.tempComboBox.currentIndexChanged['int'].connect(self.temp_unit_change)

同理,选择不同的温度单位时,执行自定义的temp_unit_change方法。

  • query_action方法中,调用前面自己写的API方法,获得天气数据

判断目前选择了哪个接口,然后调用对应的接口方法

if self.apiComboBox.currentIndex() == 0:
            self.dict_weather_now = api_tp.get_weather_now(user_input)
            self.dict_weather_forecast = api_tp.get_weather_forecast(user_input)

results matching ""

    No results matching ""