0
votes

I created a calculator for the Pythagorean Theorem with python and decided I should make a GUI for it. I wanted something where I could drag and drop the GUI as I have tried Tkinter before and it is really hard. I created a GUI with QT designer and then started to code the buttons, but for some reason, it wasn't working with Pycharm I got the error ImportError: DLL load failed: The specified module could not be found every time I ran the code, so if you can fix that please reply. When I tried to run it in MU text editor it worked fine. So I started coding the buttons to do something, but when I run the code the window is empty. Please Help. Thanks, Famous8. This is my first post :)

The Code:

import sys 
from PyQt5.QtWidgets import (QApplication, QMainWindow,)
from PyQt5 import QtWidgets

import PTCGUI

class MainWindow:
    def __init__(self):
        self.main_win = QMainWindow()
        self.ui = PTCGUI()
        self.ui.setupUi(self.main_win)
        
        self.ui.stackedWidget.setCurrentWidget(self.ui.home)
        
        self.ui.pushButton.clicked.connect(self.sqrnum)
        self.ui.pushButton_4.clicked.connect(self.sqrtnum)
        self.ui.pushButton_3.clicked.connect(self.addnum)
        self.ui.pushButton_2.clicked.connect(self.sidea)
        self.ui.pushButton_5.clicked.connect(self.sideb)
        self.ui.pushButton_6.clicked.connect(self.sidec)
        self.ui.pushButton_7.clicked.connect(self.dettri)
        self.ui.pushButton_8.clicked.connect(self.settings)
        
        
    def show(self):
            self.main_win.show()
            
    def sqrnum(self):
        self.ui.stackedWidget.setCurrentWidget(self.ui.Square)
        
    def sqrtnum(self):
        self.ui.stackedWidget.setCurrentWidget(self.ui.sqrt)
        
    def addnum(self):
        self.ui.stackedWidget.setCurrentWidget(self.ui.add)
        
    def sidea(self):
        self.ui.stackedWidget.setCurrentWidget(self.ui.sidea)
        
    def sideb(self):
        self.ui.stackedWidget.setCurrentWidget(self.ui.sideb)
        
    def sidec(self):
        self.ui.stackedWidget.setCurrentWidget(self.ui.sidec)
        
    def dettri(self):
        self.ui.stackedWidget.setCurrentWidget(self.ui.righttriangle)
        
    def settings(self):
        self.ui.stackedWidget.setCurrentWidget(self.ui.Settings)
            
if __name__ == "__main__":

    app = QtWidgets.QApplication(sys.argv)
    MainWindow = QtWidgets.QMainWindow()

    MainWindow.show()
    sys.exit(app.exec_())


Also here is the contents of PTCGUI, the file with all the info

#
# WARNING! All changes made in this file will be lost!

from PyQt5 import QtCore, QtGui, QtWidgets

class Ui_MainWindow(object):
    def setupUi(self, MainWindow):
        MainWindow.setObjectName("MainWindow")
        MainWindow.resize(939, 606)
        self.centralwidget = QtWidgets.QWidget(MainWindow)
        self.centralwidget.setObjectName("centralwidget")
        self.pushButton = QtWidgets.QPushButton(self.centralwidget)
        self.pushButton.setGeometry(QtCore.QRect(30, 460, 131, 41))
        self.pushButton.setObjectName("pushButton")
        self.pushButton_2 = QtWidgets.QPushButton(self.centralwidget)
        self.pushButton_2.setGeometry(QtCore.QRect(450, 460, 131, 41))
        self.pushButton_2.setObjectName("pushButton_2")
        self.pushButton_3 = QtWidgets.QPushButton(self.centralwidget)
        self.pushButton_3.setGeometry(QtCore.QRect(310, 460, 131, 41))
        self.pushButton_3.setObjectName("pushButton_3")
        self.pushButton_4 = QtWidgets.QPushButton(self.centralwidget)
        self.pushButton_4.setGeometry(QtCore.QRect(170, 460, 131, 41))
        self.pushButton_4.setObjectName("pushButton_4")
        self.pushButton_5 = QtWidgets.QPushButton(self.centralwidget)
        self.pushButton_5.setGeometry(QtCore.QRect(590, 460, 131, 41))
        self.pushButton_5.setObjectName("pushButton_5")
        self.pushButton_6 = QtWidgets.QPushButton(self.centralwidget)
        self.pushButton_6.setGeometry(QtCore.QRect(730, 460, 131, 41))
        self.pushButton_6.setObjectName("pushButton_6")
        self.label_2 = QtWidgets.QLabel(self.centralwidget)
        self.label_2.setGeometry(QtCore.QRect(740, 560, 191, 16))
        self.label_2.setObjectName("label_2")
        self.label_3 = QtWidgets.QLabel(self.centralwidget)
        self.label_3.setGeometry(QtCore.QRect(20, 560, 91, 16))
        self.label_3.setObjectName("label_3")
        self.pushButton_7 = QtWidgets.QPushButton(self.centralwidget)
        self.pushButton_7.setGeometry(QtCore.QRect(30, 510, 131, 41))
        self.pushButton_7.setObjectName("pushButton_7")
        self.stackedWidget = QtWidgets.QStackedWidget(self.centralwidget)
        self.stackedWidget.setGeometry(QtCore.QRect(30, 40, 871, 401))
        self.stackedWidget.setObjectName("stackedWidget")
        self.sideb = QtWidgets.QWidget()
        self.sideb.setObjectName("sideb")
        self.lineEdit_25 = QtWidgets.QLineEdit(self.sideb)
        self.lineEdit_25.setGeometry(QtCore.QRect(200, 330, 81, 31))
        self.lineEdit_25.setObjectName("lineEdit_25")
        self.lineEdit_26 = QtWidgets.QLineEdit(self.sideb)
        self.lineEdit_26.setGeometry(QtCore.QRect(110, 330, 81, 31))
        self.lineEdit_26.setObjectName("lineEdit_26")
        self.lineEdit_27 = QtWidgets.QLineEdit(self.sideb)
        self.lineEdit_27.setGeometry(QtCore.QRect(20, 330, 81, 31))
        self.lineEdit_27.setObjectName("lineEdit_27")
        self.pushButton_23 = QtWidgets.QPushButton(self.sideb)
        self.pushButton_23.setGeometry(QtCore.QRect(120, 360, 61, 23))
        self.pushButton_23.setObjectName("pushButton_23")
        self.pushButton_24 = QtWidgets.QPushButton(self.sideb)
        self.pushButton_24.setGeometry(QtCore.QRect(30, 360, 61, 23))
        self.pushButton_24.setObjectName("pushButton_24")
        self.label_9 = QtWidgets.QLabel(self.sideb)
        self.label_9.setGeometry(QtCore.QRect(0, 310, 101, 16))
        self.label_9.setObjectName("label_9")
        self.label_10 = QtWidgets.QLabel(self.sideb)
        self.label_10.setGeometry(QtCore.QRect(110, 310, 101, 16))
        self.label_10.setObjectName("label_10")
        self.stackedWidget.addWidget(self.sideb)
        self.sidec = QtWidgets.QWidget()
        self.sidec.setObjectName("sidec")
        self.lineEdit_28 = QtWidgets.QLineEdit(self.sidec)
        self.lineEdit_28.setGeometry(QtCore.QRect(20, 330, 81, 31))
        self.lineEdit_28.setObjectName("lineEdit_28")
        self.lineEdit_29 = QtWidgets.QLineEdit(self.sidec)
        self.lineEdit_29.setGeometry(QtCore.QRect(110, 330, 81, 31))
        self.lineEdit_29.setObjectName("lineEdit_29")
        self.lineEdit_30 = QtWidgets.QLineEdit(self.sidec)
        self.lineEdit_30.setGeometry(QtCore.QRect(200, 330, 81, 31))
        self.lineEdit_30.setObjectName("lineEdit_30")
        self.pushButton_25 = QtWidgets.QPushButton(self.sidec)
        self.pushButton_25.setGeometry(QtCore.QRect(30, 360, 61, 23))
        self.pushButton_25.setObjectName("pushButton_25")
        self.pushButton_26 = QtWidgets.QPushButton(self.sidec)
        self.pushButton_26.setGeometry(QtCore.QRect(120, 360, 61, 23))
        self.pushButton_26.setObjectName("pushButton_26")
        self.label_11 = QtWidgets.QLabel(self.sidec)
        self.label_11.setGeometry(QtCore.QRect(110, 310, 101, 16))
        self.label_11.setObjectName("label_11")
        self.label_14 = QtWidgets.QLabel(self.sidec)
        self.label_14.setGeometry(QtCore.QRect(10, 310, 101, 16))
        self.label_14.setObjectName("label_14")
        self.stackedWidget.addWidget(self.sidec)
        self.righttriangle = QtWidgets.QWidget()
        self.righttriangle.setObjectName("righttriangle")
        self.lineEdit_31 = QtWidgets.QLineEdit(self.righttriangle)
        self.lineEdit_31.setGeometry(QtCore.QRect(30, 350, 81, 31))
        self.lineEdit_31.setObjectName("lineEdit_31")
        self.lineEdit_32 = QtWidgets.QLineEdit(self.righttriangle)
        self.lineEdit_32.setGeometry(QtCore.QRect(160, 350, 81, 31))
        self.lineEdit_32.setObjectName("lineEdit_32")
        self.lineEdit_33 = QtWidgets.QLineEdit(self.righttriangle)
        self.lineEdit_33.setGeometry(QtCore.QRect(280, 350, 81, 31))
        self.lineEdit_33.setObjectName("lineEdit_33")
        self.lineEdit_34 = QtWidgets.QLineEdit(self.righttriangle)
        self.lineEdit_34.setGeometry(QtCore.QRect(390, 350, 161, 31))
        self.lineEdit_34.setObjectName("lineEdit_34")
        self.label_15 = QtWidgets.QLabel(self.righttriangle)
        self.label_15.setGeometry(QtCore.QRect(150, 330, 101, 16))
        self.label_15.setObjectName("label_15")
        self.label_16 = QtWidgets.QLabel(self.righttriangle)
        self.label_16.setGeometry(QtCore.QRect(270, 330, 101, 16))
        self.label_16.setObjectName("label_16")
        self.label_17 = QtWidgets.QLabel(self.righttriangle)
        self.label_17.setGeometry(QtCore.QRect(20, 330, 101, 16))
        self.label_17.setObjectName("label_17")
        self.stackedWidget.addWidget(self.righttriangle)
        self.Settings = QtWidgets.QWidget()
        self.Settings.setObjectName("Settings")
        self.label_18 = QtWidgets.QLabel(self.Settings)
        self.label_18.setGeometry(QtCore.QRect(50, 270, 101, 16))
        font = QtGui.QFont()
        font.setPointSize(12)
        self.label_18.setFont(font)
        self.label_18.setObjectName("label_18")
        self.label_19 = QtWidgets.QLabel(self.Settings)
        self.label_19.setGeometry(QtCore.QRect(50, 290, 481, 16))
        self.label_19.setObjectName("label_19")
        self.stackedWidget.addWidget(self.Settings)
        self.Square = QtWidgets.QWidget()
        self.Square.setObjectName("Square")
        self.lineEdit = QtWidgets.QLineEdit(self.Square)
        self.lineEdit.setGeometry(QtCore.QRect(40, 330, 81, 31))
        self.lineEdit.setObjectName("lineEdit")
        self.lineEdit_2 = QtWidgets.QLineEdit(self.Square)
        self.lineEdit_2.setGeometry(QtCore.QRect(150, 330, 81, 31))
        self.lineEdit_2.setObjectName("lineEdit_2")
        self.pushButton_10 = QtWidgets.QPushButton(self.Square)
        self.pushButton_10.setGeometry(QtCore.QRect(50, 370, 61, 23))
        self.pushButton_10.setObjectName("pushButton_10")
        self.label_4 = QtWidgets.QLabel(self.Square)
        self.label_4.setGeometry(QtCore.QRect(40, 310, 251, 16))
        self.label_4.setObjectName("label_4")
        self.label_8 = QtWidgets.QLabel(self.Square)
        self.label_8.setGeometry(QtCore.QRect(40, 280, 131, 16))
        font = QtGui.QFont()
        font.setFamily("Sitka Heading")
        font.setPointSize(12)
        self.label_8.setFont(font)
        self.label_8.setObjectName("label_8")
        self.stackedWidget.addWidget(self.Square)
        self.home_2 = QtWidgets.QWidget()
        self.home_2.setObjectName("home_2")
        self.stackedWidget.addWidget(self.home_2)
        self.sqrt = QtWidgets.QWidget()
        self.sqrt.setObjectName("sqrt")
        self.stackedWidget_2 = QtWidgets.QStackedWidget(self.sqrt)
        self.stackedWidget_2.setGeometry(QtCore.QRect(860, 10, 871, 401))
        self.stackedWidget_2.setObjectName("stackedWidget_2")
        self.home_3 = QtWidgets.QWidget()
        self.home_3.setObjectName("home_3")
        self.stackedWidget_2.addWidget(self.home_3)
        self.one_3 = QtWidgets.QWidget()
        self.one_3.setObjectName("one_3")
        self.lineEdit_5 = QtWidgets.QLineEdit(self.one_3)
        self.lineEdit_5.setGeometry(QtCore.QRect(40, 330, 81, 31))
        self.lineEdit_5.setObjectName("lineEdit_5")
        self.lineEdit_6 = QtWidgets.QLineEdit(self.one_3)
        self.lineEdit_6.setGeometry(QtCore.QRect(150, 330, 81, 31))
        self.lineEdit_6.setObjectName("lineEdit_6")
        self.pushButton_12 = QtWidgets.QPushButton(self.one_3)
        self.pushButton_12.setGeometry(QtCore.QRect(50, 370, 61, 23))
        self.pushButton_12.setObjectName("pushButton_12")
        self.label_6 = QtWidgets.QLabel(self.one_3)
        self.label_6.setGeometry(QtCore.QRect(40, 310, 251, 16))
        self.label_6.setObjectName("label_6")
        self.stackedWidget_2.addWidget(self.one_3)
        self.page_3 = QtWidgets.QWidget()
        self.page_3.setObjectName("page_3")
        self.stackedWidget_2.addWidget(self.page_3)
        self.lineEdit_17 = QtWidgets.QLineEdit(self.sqrt)
        self.lineEdit_17.setGeometry(QtCore.QRect(110, 330, 81, 31))
        self.lineEdit_17.setObjectName("lineEdit_17")
        self.lineEdit_18 = QtWidgets.QLineEdit(self.sqrt)
        self.lineEdit_18.setGeometry(QtCore.QRect(10, 330, 81, 31))
        self.lineEdit_18.setObjectName("lineEdit_18")
        self.label_12 = QtWidgets.QLabel(self.sqrt)
        self.label_12.setGeometry(QtCore.QRect(10, 310, 281, 16))
        self.label_12.setObjectName("label_12")
        self.pushButton_18 = QtWidgets.QPushButton(self.sqrt)
        self.pushButton_18.setGeometry(QtCore.QRect(20, 370, 61, 23))
        self.pushButton_18.setObjectName("pushButton_18")
        self.stackedWidget.addWidget(self.sqrt)
        self.add = QtWidgets.QWidget()
        self.add.setObjectName("add")
        self.lineEdit_19 = QtWidgets.QLineEdit(self.add)
        self.lineEdit_19.setGeometry(QtCore.QRect(20, 330, 81, 31))
        self.lineEdit_19.setObjectName("lineEdit_19")
        self.lineEdit_20 = QtWidgets.QLineEdit(self.add)
        self.lineEdit_20.setGeometry(QtCore.QRect(120, 330, 81, 31))
        self.lineEdit_20.setObjectName("lineEdit_20")
        self.pushButton_19 = QtWidgets.QPushButton(self.add)
        self.pushButton_19.setGeometry(QtCore.QRect(30, 370, 61, 23))
        self.pushButton_19.setObjectName("pushButton_19")
        self.pushButton_20 = QtWidgets.QPushButton(self.add)
        self.pushButton_20.setGeometry(QtCore.QRect(130, 370, 61, 23))
        self.pushButton_20.setObjectName("pushButton_20")
        self.lineEdit_21 = QtWidgets.QLineEdit(self.add)
        self.lineEdit_21.setGeometry(QtCore.QRect(220, 330, 81, 31))
        self.lineEdit_21.setObjectName("lineEdit_21")
        self.label_13 = QtWidgets.QLabel(self.add)
        self.label_13.setGeometry(QtCore.QRect(20, 310, 261, 16))
        self.label_13.setObjectName("label_13")
        self.stackedWidget.addWidget(self.add)
        self.sidea = QtWidgets.QWidget()
        self.sidea.setObjectName("sidea")
        self.lineEdit_22 = QtWidgets.QLineEdit(self.sidea)
        self.lineEdit_22.setGeometry(QtCore.QRect(20, 320, 81, 31))
        self.lineEdit_22.setObjectName("lineEdit_22")
        self.lineEdit_23 = QtWidgets.QLineEdit(self.sidea)
        self.lineEdit_23.setGeometry(QtCore.QRect(120, 320, 81, 31))
        self.lineEdit_23.setObjectName("lineEdit_23")
        self.lineEdit_24 = QtWidgets.QLineEdit(self.sidea)
        self.lineEdit_24.setGeometry(QtCore.QRect(220, 320, 81, 31))
        self.lineEdit_24.setObjectName("lineEdit_24")
        self.pushButton_21 = QtWidgets.QPushButton(self.sidea)
        self.pushButton_21.setGeometry(QtCore.QRect(130, 350, 61, 23))
        self.pushButton_21.setObjectName("pushButton_21")
        self.pushButton_22 = QtWidgets.QPushButton(self.sidea)
        self.pushButton_22.setGeometry(QtCore.QRect(30, 350, 61, 23))
        self.pushButton_22.setObjectName("pushButton_22")
        self.label_5 = QtWidgets.QLabel(self.sidea)
        self.label_5.setGeometry(QtCore.QRect(10, 300, 101, 16))
        self.label_5.setObjectName("label_5")
        self.label_7 = QtWidgets.QLabel(self.sidea)
        self.label_7.setGeometry(QtCore.QRect(110, 300, 101, 16))
        self.label_7.setObjectName("label_7")
        self.stackedWidget.addWidget(self.sidea)
        self.pushButton_8 = QtWidgets.QPushButton(self.centralwidget)
        self.pushButton_8.setGeometry(QtCore.QRect(170, 510, 131, 41))
        self.pushButton_8.setObjectName("pushButton_8")
        self.label = QtWidgets.QLabel(self.centralwidget)
        self.label.setGeometry(QtCore.QRect(340, -10, 251, 51))
        font = QtGui.QFont()
        font.setPointSize(12)
        self.label.setFont(font)
        self.label.setObjectName("label")
        self.label_20 = QtWidgets.QLabel(self.centralwidget)
        self.label_20.setGeometry(QtCore.QRect(330, 520, 221, 16))
        self.label_20.setObjectName("label_20")
        MainWindow.setCentralWidget(self.centralwidget)
        self.statusbar = QtWidgets.QStatusBar(MainWindow)
        self.statusbar.setObjectName("statusbar")
        MainWindow.setStatusBar(self.statusbar)

        self.retranslateUi(MainWindow)
        self.stackedWidget.setCurrentIndex(8)
        self.stackedWidget_2.setCurrentIndex(1)
        QtCore.QMetaObject.connectSlotsByName(MainWindow)

    def retranslateUi(self, MainWindow):
        _translate = QtCore.QCoreApplication.translate
        MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
        self.pushButton.setText(_translate("MainWindow", "Square a Number"))
        self.pushButton_2.setText(_translate("MainWindow", "Find A"))
        self.pushButton_3.setText(_translate("MainWindow", "Add numbers"))
        self.pushButton_4.setText(_translate("MainWindow", "Square Root a Number"))
        self.pushButton_5.setText(_translate("MainWindow", "Find B"))
        self.pushButton_6.setText(_translate("MainWindow", "Find C"))
        self.label_2.setText(_translate("MainWindow", "Copyright 2020 Zain Raza"))
        self.label_3.setText(_translate("MainWindow", "Version 1.1"))
        self.pushButton_7.setText(_translate("MainWindow", "Detect Right Triangles"))
        self.pushButton_23.setText(_translate("MainWindow", "Enter"))
        self.pushButton_24.setText(_translate("MainWindow", "Enter"))
        self.label_9.setText(_translate("MainWindow", "Please enter Side A"))
        self.label_10.setText(_translate("MainWindow", "Please enter Side C"))
        self.pushButton_25.setText(_translate("MainWindow", "Enter"))
        self.pushButton_26.setText(_translate("MainWindow", "Enter"))
        self.label_11.setText(_translate("MainWindow", "Please enter Side B"))
        self.label_14.setText(_translate("MainWindow", "Please enter Side A"))
        self.label_15.setText(_translate("MainWindow", "Please enter Side B"))
        self.label_16.setText(_translate("MainWindow", "Please enter Side C"))
        self.label_17.setText(_translate("MainWindow", "Please enter Side A"))
        self.label_18.setText(_translate("MainWindow", "Coming Soon"))
        self.label_19.setText(_translate("MainWindow", "For more information please email [email protected] or chat on Discord Famous8#6462"))
        self.pushButton_10.setText(_translate("MainWindow", "Enter"))
        self.label_4.setText(_translate("MainWindow", "Please enter the number you would like to square"))
        self.label_8.setText(_translate("MainWindow", "Square a Number"))
        self.pushButton_12.setText(_translate("MainWindow", "Enter"))
        self.label_6.setText(_translate("MainWindow", "Please enter the number you would like to square"))
        self.label_12.setText(_translate("MainWindow", "Please enter the number you would like to Square Root"))
        self.pushButton_18.setText(_translate("MainWindow", "Enter"))
        self.pushButton_19.setText(_translate("MainWindow", "Enter"))
        self.pushButton_20.setText(_translate("MainWindow", "Enter"))
        self.label_13.setText(_translate("MainWindow", "Please enter the two numbers you would like to add"))
        self.pushButton_21.setText(_translate("MainWindow", "Enter"))
        self.pushButton_22.setText(_translate("MainWindow", "Enter"))
        self.label_5.setText(_translate("MainWindow", "Please enter Side B"))
        self.label_7.setText(_translate("MainWindow", "Please enter Side C"))
        self.pushButton_8.setText(_translate("MainWindow", "Settings"))
        self.label.setText(_translate("MainWindow", "Pythagoream Theorem Calculator"))
        self.label_20.setText(_translate("MainWindow", "Please choose the action you would like to do"))


if __name__ == "__main__":
    import sys
    app = QtWidgets.QApplication(sys.argv)
    MainWindow = QtWidgets.QMainWindow()
    ui = Ui_MainWindow()
    ui.setupUi(MainWindow)
    MainWindow.show()
    sys.exit(app.exec_())
1
You are not creating an instance of your class. Change MainWindow = QtWidgets.QMainWindow();MainWindow.show() to mainWindow = MainWindow(); mainWindow.show()musicamante
I did what you said but now I have two more errors: Traceback (most recent call last): File "c:\users\owner\youdontneedtoknowthefullpath", line 55, in <module> mainWindow = MainWindow() File "c:\users\owner\youdontneedtoknowthefullpath", line 10, in init self.ui = PTCGUI() TypeError: 'module' object is not callableFamous8
What are the contents of PTCGUI?Heike
Please edit your code to provide a minimal, reproducible example, including the UI file you're trying to import. Finally, check the code formatting in the question preview (read more about formatting code).musicamante
I have added the contents of PTCGUI into the question sorry im new to Stack Overflow so Im not good at formatting text.Famous8

1 Answers

0
votes

In your code:

  1. you are not subclassing your MainWindow. In name == main part. You need MainWindow() not QtWidgets.QMainWindow()

I would also suggest you name your variable differently than your class

if __name__ == "__main__":

    app = QtWidgets.QApplication(sys.argv)
    MyWindow = MainWindow()
    MyWindow.show()
    sys.exit(app.exec_())
  1. Your class should subclass QMainWindow. And your init should starts like this

    class MainWindow(QMainWindow):
        def __init__(self):
            super().__init__()
            self.ui = PTCGUI.Ui_MainWindow()
            self.ui.setupUi(self)
    
  2. Remove def show(self). You don't need to call self.ui.show(), you want to call just self.show() directly.