簡體   English   中英

如何在 Python 中使用 PyQT5 在主窗口中打開新的 Window?

[英]How Open a new Window in Mainwindow with PyQT5 in Python?

我在編程方面是全新的。 我想為 Windows 創建一個抽認卡應用程序。 到目前為止,我可以告訴我的“程序”工作正常,但是當我點擊我的小部件時,我無法打開一個新的 Window。 我需要知道如何准確而具體地編寫代碼 - 我必須在哪里添加確切的內容。 在新的 Window 內部,我想要一個文本字段,我可以在其中寫一些筆記,選擇是否要將文本寫為“強”、“彎曲”之類的東西。

我在文檔中進行了研究,嘗試了一些東西,但它並沒有像應有的那樣工作。 我希望有人能幫我解決這個“問題”。當我點擊“甲板”和其他按鈕時,我想創建一個新的 Window,我有。 否則我想在左上角有一個菜單,我可以點擊“新甲板”或“幫助”或類似的東西。

這是我的代碼

from PyQt5 import QtWidgets, QtGui, QtCore
import sys

class MyWindow(QtWidgets.QMainWindow):
def __init__(self):
    super(MyWindow, self).__init__()

    self.initUI()

    self.setGeometry(100, 100, 1280, 900)
    self.setWindowTitle("Flashcards!")



def initUI(self):
    wid = QtWidgets.QWidget(self)
    self.setCentralWidget(wid)

    self.screenObject = QtWidgets.QDesktopWidget().screenGeometry(0)


    self.line = QtWidgets.QFrame(self)
    self.line.setGeometry(QtCore.QRect(0, 0, self.screenObject.width(), 2))
    self.line.setStyleSheet("border: 1px solid rgb(170, 170, 170)")

    self.line_2 = QtWidgets.QFrame(self)
    self.line_2.setGeometry(QtCore.QRect(0, 35, self.screenObject.width(), 2))
    self.line_2.setStyleSheet("border: 1px solid rgb(170, 170, 170)")


    self.topfont = QtGui.QFont()
    self.topfont.setBold(True)
    self.topfont.setWeight(75)
    self.topfont.setPointSize(9)

###########紐扣###################################### ##############

    self.deckLabel = QtWidgets.QLabel(self)
    self.deckLabel.setText("Decks")
    self.deckLabel.setFont(self.topfont)
    self.deckLabel.setStyleSheet("QLabel::hover {color: rgb(100, 100, 100)}")
    self.deckLabel.mousePressEvent = self.clicked




    self.addLabel = QtWidgets.QLabel(self)
    self.addLabel.setText("Add")
    self.addLabel.setFont(self.topfont)
    self.addLabel.setStyleSheet("QLabel::hover {color: rgb(100, 100, 100)}")
    self.addLabel.mousePressEvent = self.clicked


    self.browseLabel = QtWidgets.QLabel(self)
    self.browseLabel.setText("Browse")
    self.browseLabel.setFont(self.topfont)
    self.browseLabel.setStyleSheet("QLabel::hover {color: rgb(100, 100, 100)}")
    self.browseLabel.mousePressEvent = self.clicked


    self.statsLabel = QtWidgets.QLabel(self)
    self.statsLabel.setText("Stats")
    self.statsLabel.setFont(self.topfont)
    self.statsLabel.setStyleSheet("QLabel::hover {color: rgb(100, 100, 100)}")
    self.statsLabel.mousePressEvent = self.clicked


    self.syncLabel = QtWidgets.QLabel(self)
    self.syncLabel.setText("Sync")
    self.syncLabel.setFont(self.topfont)
    self.syncLabel.setStyleSheet("QLabel::hover {color: rgb(100, 100, 100)}")
    self.syncLabel.mousePressEvent = self.clicked


    self.hbox = QtWidgets.QHBoxLayout()
    self.hbox.setSpacing(60)
    self.hbox.addStretch()
    self.hbox.addWidget(self.deckLabel)
    self.hbox.addWidget(self.addLabel)
    self.hbox.addWidget(self.browseLabel)
    self.hbox.addWidget(self.statsLabel)
    self.hbox.addWidget(self.syncLabel)
    self.hbox.addStretch()
    self.hbox.setAlignment(QtCore.Qt.AlignTop)

    wid.setLayout(self.hbox)




def clicked(self, event):
    print("clicked!")


app = QtWidgets.QApplication(sys.argv)
win = MyWindow()
win.show()
sys.exit(app.exec_())

我最近有一個類似的問題。 我希望在單擊按鈕時顯示一個彈出窗口 window。 為此,我只是創建了一個 function,當您單擊按鈕時會觸發它。 與您的clicked() function 非常相似。

在此 function 中,您可以定義新的 window 參數並顯示它。 請注意,如果要生成此 window 並繼續處理代碼,可以使用.show() .exec_()如果要等待 window 在繼續執行代碼之前關閉,則可以使用 .exec_()。

就我而言,它顯示了一個彈出窗口 window 以警告用戶需要重新啟動:

def restart(self):
    # Create a popup window
    self.w = QMessageBox()

    # Custom Texts
    self.w.setWindowTitle("GUI Version Check")
    self.w.setText("A newer version of the GUI is available.")

    # Custom Icons
    # use a file logo.svg to replace the default WindowIcon
    self.w.setWindowIcon(QtGui.QIcon('logo.svg'))
    # Add a Warning Icon on the new window
    self.w.setIcon(QMessageBox.Warning)

    # Custom Button: rename Yes by Restart
    self.w.setStandardButtons(QMessageBox.Yes)
    b = self.w.button(QMessageBox.Yes)
    b.setText("Restart")

    # Start the window and wait for a user input
    self.w.exec_()

    # In my case restart the GUI here

暫無
暫無

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

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