微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

在QLineEdit中接受一个用户输入后,如何获得新的用户输入?

如何解决在QLineEdit中接受一个用户输入后,如何获得新的用户输入?

我有一个程序,可以在满足某些条件时执行特定任务。

我希望QLineEdit像python的input()函数一样工作。我希望这样做,以便程序在继续执行代码之前停止为(新)用户输入。

MainGui.py (一小部分/用于理解的最小代码

From PyQt5.QtWidgets import QLabel,QLineEdit,QPushButton,QStackedWidget,QMainWindow,QApplication
from PyQt5.QtCore import Qt,QEvent
from PyQt5 import QtCore,QtGui,QtWidgets
from PyQt5.uic import loadUiType
import sys
import Weather

ui,_ = loadUiType('icons\\Bot.ui')

class MainApp(QMainWindow,ui):
    def __init__(self):
        QMainWindow.__init__(self)
        self.setupUi(self)
        self.setwindowFlag(Qt.FramelessWindowHint)  #Removes Title bar
        self.setAttribute(Qt.WA_TranslucentBackground) #Removes the remaining border
        self.Handle_Text()

    def setTB(self,text):
        OP = self.OutputTB.setText(text)

    def Handle_Text(self):
        self.lineEdit.returnpressed.connect(self.process)

    def process(self):
        sentence = self.lineEdit.text().lower() #user inputs 'weather'

        if "weather" in sentence: #true
            self.setTB("which City ?")
            #Take the userinput for city here
            City = self.lineEdit.text() #stores weather [problem line]
            self.setTB(Weather.weatherreport(City))
        else:
            self.setTB("Enter an actual city please.")
        self.lineEdit.clear()

def main():
    app = QApplication(sys.argv)
    window = MainApp()
    window.show()
    app.exec_()

if __name__ == '__main__':
    main()

我想在这里做的是,我希望程序在询问“哪个城市?”之后等待。以便用户可以输入城市(例如伦敦),在获得输入后,程序会将其存储在名为“ City”的变量中,然后将该值传递给“ Weather.weatherreport(City)” [weatherreport是一个函数从下面的另一个文件中] 当前,程序将考虑LineEdit中已经存在的文本。我已经尝试为城市的第二个输入专门创建功能,但是程序会跳过它们并继续前进。

更多说明(逐步):

  1. 用户在QlineEdit中输入一个字符串,其中包含“天气”单词 例如,“什么天气?”

  2. 程序在此行的帮助下检测到给定的字符串中有天气 如果句子中是“天气”:

  3. 然后,该程序将在Qtextbrowser中显示一个询问城市名称的问题。 “ self.setTB(“哪个城市?”)' [((setTB是名为'OutputTB'的QTextbrowser的函数

  4. 此后,用户将在他们输入天气字符串的同一LineEdit中输入城市名称(从步骤1开始)

  5. 新输入(城市名称输入)将存储在名为“ City” Ex的变量中。用户进入伦敦。

  6. 包含城市名称的变量City将随后通过以下方式从导入的模块传递给函数: Weather.weatherreport(City)

  7. 该城市的温度将显示在QTextbrowser(名为OutputTB)上

第4步是我不太正确的部分,因为lineEdit会保留第1步的输入(天气是什么)并将其传递给weatherreport函数。由于这不是城市名称,因此weatherreport函数会给我一个错误

Weather.py

import requests

def weatherreport(City):
    #Url Components 
    BASE_URL = "https://api.openweathermap.org/data/2.5/weather?"
    City = City.capitalize()
    API_KEY = "[API Key from OpenWeatherMap]"

    # Joining the Url components to makeup a URL
    # '&units=metrics' converts the temperature from kelvin to celcius
    URL = BASE_URL + "q=" + City + "&appid=" + API_KEY + "&units=metric"

    response = requests.get(URL)

    if response.status_code == 200:
        data = response.json()
        main = data['main']

        temperature = main['temp']
        humidity = main['humidity']
        pressure = main['pressure']
        report = data['weather']
        
        return("\n" + City.capitalize())
        
        return(f"Temperature: {temperature} degree Celcius")

        return(f"Humidity: {humidity}%"))

        return(f"Atmospheric Pressure: {pressure} hecto Pascals")

        return(f"Weather : {report[0]['description']}")
    else:
        # showing the error message
        return("Error in the HTTP request")

Ui页面,Textbrowser在LineEdit上方

我已经看到了关于SO的其他问题,但是没有一个问题描述了这个特殊情况。

我也从@eyllanesc看到了许多答案。我正在使用pyqt5。

完整项目的GitHub链接https://github.com/SaadMansuri3/AVA

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。