1

im totally new when it comes to programming. I want to create a Flashcard Application for Windows. So far i can tell my "programm" works fine, but i cant manage to open a new Window when i click on my widgets. I need to know how i can write the code corectly and specificly - where do i have to add what exactly. Inside the new Window i would like to have a Textfield where i can write some notes, choose if i want to write the Text as "strong", "curved" and stuff like this.

I researched in the docs, tried something and it doesnt work like it should be. I hope someone can help me to solve this "problem".I want to create a new Window when i click on "Decks" and the other buttons, that i have. Else i would like to have a Menu in the top left Corner where i can click on "New Deck" or "Help" or something like this.

Here is my Code

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)

###########Buttons####################################################

    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_())
MrZellular
  • 11
  • 2

1 Answers1

0

I recently had a similar issue. I wanted a popup window to be displayed when I clicked on a button. To do so, I simply created a function that is triggered when you click on the button. Very similar to your clicked() function.

In this function, you can define your new window parameters and display it. Please note that you can use .show() if you want to generate this window and continue to process the code or .exec_() if you want to wait for the window to be closed before continuing the execution of your code.

In my case, it was displaying a popup window to warn the user that a restart is needed:

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
Romain Capron
  • 1,565
  • 1
  • 18
  • 23
  • Hi, thx for your Answer. I tried it out but its not what i was looking for. If i click onto my Widgets a second Window should open with a Textfield, where i can enter some Notes. – MrZellular Jul 31 '22 at 06:52
  • Well, this is how to generate a second window. You can configure it to be whatever you want. For example add a text field and a save button. – Romain Capron Aug 02 '22 at 06:00