簡體   English   中英

如何將數據從我的 PyQt5 UI 輸入傳遞到我的 python 程序?

[英]How can I pass data from my PyQt5 UI input to my python program?

所以我制作了我的 python 程序,這是一個籃球游戲模擬器,它接受終端用戶的輸入,我想給它添加一個 UI,所以我現在正試圖將它連接到 PyQt5 並為其提供一個很好的顯示。 問題是,當我啟動我的程序時,我希望程序等待用戶在 lineEdit 中輸入內容並單擊 Enter,然后使用用戶的輸入運行模擬。 但最終發生的情況是,一旦我啟動程序,UI 就會打開,然后模擬的 rest 會立即運行,因此用戶沒有時間在 lineEdit 中輸入任何內容。

在我的 UI class 中,我有 UI 的代碼,然后這就是我的 lineEdit 和 lineEdit 輸入按鈕

class Ui_MainWindow(object):
    def setupUi(self, MainWindow):
        #40 lines of code for the UI....
 
        self.enterButton.clicked.connect(self.enterPressed)

def enterPressed(self):
    userInput = self.lineEdit.text()
    self.lineEdit.clear()
    return userInput

然后這里是主程序代碼的開頭,需要用戶輸入 UI

year = int(input("Enter year between 1980-2022: "))

while year < 1980 and year > 2022:
    year = int(input("Invalid input, enter year between 1980-2022: "))
            
yearStandings = requests.get("https://www.basketball-reference.com/leagues/NBA_" + str(year) + "_standings.html").text

我不想通過終端向用戶詢問輸入,而是想為變量“year”分配放在 lineEdit 中的值。 我怎樣才能做到這一點?

# IMPORTS

class Ui_MainWindow(object):
    def self(self, MainWindow):
         # 40 lines of code
         self.enterButton.clicked.connect(self.enterPressed)
    def enterPressed(self):
         year = int(self.lineEdit.text())
         if year<1980 or year>2022 :
            print("Invalid input, enter year between 1980-2022: ")
            return None
         global yearStandings
         yearStandings = requests.get("https://www.basketball-reference.com/leagues/NBA_" + str(year) + "_standings.html").text

# And Remaining Code with pyuic5 code generated with -x argument

然后,您可以在將信息存儲在yearStandings變量中后關閉 GUI(如果您願意)並將其打印在終端或您想要的任何內容中

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM