尝试在python

时间:2017-04-30 12:20:47

标签: python python-3.x pyqt pyqt5

如我所述,我目前正在为荷兰Exloo的游泳池“De Leewal”建立一个数据库接口

好吧,我正在尝试让这个人能够切换屏幕(使用按钮,像“返回主菜单”)

但是当我连接代码并按下按钮时,我得到一个屏幕说:Python崩溃了(荷兰语) ... 有没有人发现错误?

这是我的代码:

MainWindow.py:

# -*- coding: utf-8 -*-

# Form implementation generated from reading ui file 'MainWindow.ui'
#
# Created by: PyQt5 UI code generator 5.5.1
#
# WARNING! All changes made in this file will be lost!

from PyQt5 import QtCore, QtGui, QtWidgets
import sys
import mysql.connector as mariadb
import ButtonFunctions

global mariadb_connection
global cursor
mariadb_connection = mariadb.connect(user='root', password='root', database='leewaldb')
cursor = mariadb_connection.cursor()

class Ui_MainWindow(object):
    def setupUi(self, MainWindow):
        MainWindow.setObjectName("MainWindow")
        MainWindow.resize(407, 394)
        icon = QtGui.QIcon()
        icon.addPixmap(QtGui.QPixmap("media/Icon.png"), QtGui.QIcon.Normal, QtGui.QIcon.Off)
        MainWindow.setWindowIcon(icon)
        MainWindow.setStyleSheet("background-color: qlineargradient(spread:pad, x1:0, y1:0, x2:1, y2:0, stop:0 rgba(9, 41, 4, 255), stop:0.085 rgba(2, 79, 0, 255), stop:0.19 rgba(50, 147, 22, 255), stop:0.275 rgba(236, 191, 49, 255), stop:0.39 rgba(243, 61, 34, 255), stop:0.555 rgba(135, 81, 60, 255), stop:0.667 rgba(121, 75, 255, 255), stop:0.825 rgba(164, 255, 244, 255), stop:0.885 rgba(104, 222, 71, 255), stop:1 rgba(93, 128, 0, 255));")
        self.centralwidget = QtWidgets.QWidget(MainWindow)
        self.centralwidget.setObjectName("centralwidget")
        self.gridLayout_2 = QtWidgets.QGridLayout(self.centralwidget)
        self.gridLayout_2.setObjectName("gridLayout_2")
        self.gridLayout = QtWidgets.QGridLayout()
        self.gridLayout.setObjectName("gridLayout")
        self.CopyRightLabel = QtWidgets.QLabel(self.centralwidget)
        self.CopyRightLabel.setObjectName("CopyRightLabel")
        self.gridLayout.addWidget(self.CopyRightLabel, 1, 0, 1, 1)
        self.ExitButton = QtWidgets.QCommandLinkButton(self.centralwidget)
        self.ExitButton.setObjectName("ExitButton")
        self.gridLayout.addWidget(self.ExitButton, 2, 0, 1, 1)
        self.ShowEverythingButton = QtWidgets.QCommandLinkButton(self.centralwidget)
        self.ShowEverythingButton.setObjectName("ShowEverythingButton")
        self.gridLayout.addWidget(self.ShowEverythingButton, 4, 0, 1, 1)
        self.DeletePersonButton = QtWidgets.QCommandLinkButton(self.centralwidget)
        self.DeletePersonButton.setStyleSheet("")
        self.DeletePersonButton.setObjectName("DeletePersonButton")
        self.gridLayout.addWidget(self.DeletePersonButton, 7, 0, 1, 1)
        self.WelcomeLabel = QtWidgets.QLabel(self.centralwidget)
        self.WelcomeLabel.setObjectName("WelcomeLabel")
        self.gridLayout.addWidget(self.WelcomeLabel, 0, 0, 1, 1)
        self.AddPersonButton = QtWidgets.QCommandLinkButton(self.centralwidget)
        self.AddPersonButton.setObjectName("AddPersonButton")
        self.gridLayout.addWidget(self.AddPersonButton, 6, 0, 1, 1)
        self.SearchSpecificButton = QtWidgets.QCommandLinkButton(self.centralwidget)
        self.SearchSpecificButton.setObjectName("SearchSpecificButton")
        self.gridLayout.addWidget(self.SearchSpecificButton, 5, 0, 1, 1)
        self.gridLayout_2.addLayout(self.gridLayout, 0, 0, 1, 1)
        MainWindow.setCentralWidget(self.centralwidget)

        self.retranslateUi(MainWindow)
        QtCore.QMetaObject.connectSlotsByName(MainWindow)

    def retranslateUi(self, MainWindow):
        _translate = QtCore.QCoreApplication.translate
        MainWindow.setWindowTitle(_translate("MainWindow", "BezoekersDatabase Leewal Hoofdmenu"))
        self.CopyRightLabel.setText(_translate("MainWindow", "© N.S. Geldorp & H. Nahumury 2017"))
        self.ExitButton.setText(_translate("MainWindow", "Exit"))
        self.ShowEverythingButton.setText(_translate("MainWindow", "Iedereen geregistreerd in deze database"))
        self.DeletePersonButton.setText(_translate("MainWindow", "Verwijder iemand uit de database"))
        self.WelcomeLabel.setText(_translate("MainWindow", "Welkom bij de bezoekersdatabase voor het zwembad \"De Leewal\" te Exloo"))
        self.AddPersonButton.setText(_translate("MainWindow", "Voeg iemand toe aan de database"))
        self.SearchSpecificButton.setText(_translate("MainWindow", "Zoek een specifiek persoon"))
        self.ExitButton.clicked.connect(ButtonFunctions.MainWindow.ButtonExitPressed)
        self.ShowEverythingButton.clicked.connect(ButtonFunctions.MainWindow.ButtonShowEverythingPressed)
        self.DeletePersonButton.clicked.connect(ButtonFunctions.MainWindow.ButtonDeletePersonPressed)
        self.AddPersonButton.clicked.connect(ButtonFunctions.MainWindow.ButtonAddPersonPressed)
        self.SearchSpecificButton.clicked.connect(ButtonFunctions.MainWindow.ButtonSearchSpecificPressed)

def mainforbuttonfunctions():
    app = QtWidgets.QApplication(sys.argv)
    ex = TheMainWindow.Ui_MainWindow()
    w = QtWidgets.QMainWindow()
    ex.setupUi(w)
    w.show()
    sys.exit(app.exec_())    

def main():
    app = QtWidgets.QApplication(sys.argv)
    ex = Ui_MainWindow()
    w = QtWidgets.QMainWindow()
    ex.setupUi(w)
    w.show()
    sys.exit(app.exec_())

if __name__ == "__main__":
    main()

ButtonFunctions.py:

import mysql.connector as mariadb
import MainWindow as TheMainWindow
import sys

global mariadb_connection
global cursor
mariadb_connection = mariadb.connect(user='root', password='root', database='leewaldb')
cursor = mariadb_connection.cursor()

class MainWindow():
    def ButtonExitPressed():
        cursor.execute("shutdown")
        exit()

    def ButtonShowEverythingPressed():
        pass

    def ButtonDeletePersonPressed():
        pass

    def ButtonAddPersonPressed():
        pass

    def ButtonSearchSpecificPressed():
        pass

class ShowEveryone():
    def ButtonBackToMainMenuPressed():
        self.close
        TheMainWindow.mainforbuttonfunctions()

ShowEveryone.py:

# -*- coding: utf-8 -*-

# Form implementation generated from reading ui file 'ShowEveryone.ui'
#
# Created by: PyQt5 UI code generator 5.5.1
#
# WARNING! All changes made in this file will be lost!

from PyQt5 import QtCore, QtGui, QtWidgets
import sys
import ButtonFunctions
import MainWindow as TheMainWindow

class Ui_ShowEveryoneInDatabase(object):
    def setupUi(self, ShowEveryoneInDatabase):
        ShowEveryoneInDatabase.setObjectName("ShowEveryoneInDatabase")
        ShowEveryoneInDatabase.resize(409, 396)
        icon = QtGui.QIcon()
        icon.addPixmap(QtGui.QPixmap("media/Icon.png"), QtGui.QIcon.Normal, QtGui.QIcon.Off)
        ShowEveryoneInDatabase.setWindowIcon(icon)
        ShowEveryoneInDatabase.setStyleSheet("background-color: qlineargradient(spread:pad, x1:0, y1:0, x2:1, y2:0, stop:0 rgba(9, 41, 4, 255), stop:0.085 rgba(2, 79, 0, 255), stop:0.19 rgba(50, 147, 22, 255), stop:0.275 rgba(236, 191, 49, 255), stop:0.39 rgba(243, 61, 34, 255), stop:0.555 rgba(135, 81, 60, 255), stop:0.667 rgba(121, 75, 255, 255), stop:0.825 rgba(164, 255, 244, 255), stop:0.885 rgba(104, 222, 71, 255), stop:1 rgba(93, 128, 0, 255));")
        self.centralwidget = QtWidgets.QWidget(ShowEveryoneInDatabase)
        self.centralwidget.setObjectName("centralwidget")
        self.gridLayout_2 = QtWidgets.QGridLayout(self.centralwidget)
        self.gridLayout_2.setObjectName("gridLayout_2")
        self.LijstWidget = QtWidgets.QListWidget(self.centralwidget)
        self.LijstWidget.setStyleSheet("color: rgb(0, 0, 0);")
        self.LijstWidget.setObjectName("LijstWidget")
        self.gridLayout_2.addWidget(self.LijstWidget, 1, 0, 1, 1)
        self.ReturnToMainMenuButton = QtWidgets.QCommandLinkButton(self.centralwidget)
        self.ReturnToMainMenuButton.setObjectName("ReturnToMainMenuButton")
        self.gridLayout_2.addWidget(self.ReturnToMainMenuButton, 2, 0, 1, 1)
        self.gridLayout = QtWidgets.QGridLayout()
        self.gridLayout.setObjectName("gridLayout")
        self.IedereenInDatabaseLabel = QtWidgets.QLabel(self.centralwidget)
        self.IedereenInDatabaseLabel.setObjectName("IedereenInDatabaseLabel")
        self.gridLayout.addWidget(self.IedereenInDatabaseLabel, 0, 0, 1, 1)
        self.gridLayout_2.addLayout(self.gridLayout, 0, 0, 1, 1)
        ShowEveryoneInDatabase.setCentralWidget(self.centralwidget)

        self.retranslateUi(ShowEveryoneInDatabase)
        QtCore.QMetaObject.connectSlotsByName(ShowEveryoneInDatabase)

    def retranslateUi(self, ShowEveryoneInDatabase):
        _translate = QtCore.QCoreApplication.translate
        ShowEveryoneInDatabase.setWindowTitle(_translate("ShowEveryoneInDatabase", "ToonIedereen - Leewal Database"))
        self.ReturnToMainMenuButton.setText(_translate("ShowEveryoneInDatabase", "Terug naar het hoofdmenu"))
        self.IedereenInDatabaseLabel.setText(_translate("ShowEveryoneInDatabase", "Iedereen in de database:"))
        self.ReturnToMainMenuButton.clicked.connect(ButtonFunctions.ShowEveryone.ButtonBackToMainMenuPressed)

def main():
    app = QtWidgets.QApplication(sys.argv)
    ex = Ui_ShowEveryoneInDatabase()
    w = QtWidgets.QMainWindow()
    ex.setupUi(w)
    w.show()
    sys.exit(app.exec_())

if __name__ == "__main__":
    main()

提前致谢。

-Natan

P.S。如果你留下了一个downvote,请留下一个理由,如果我修理它或者证明离开它是不公平的,请准备好将其删除。

1 个答案:

答案 0 :(得分:0)

您的代码非常混乱。下面是ShowEveryone.py重写的。它是此示例中唯一需要的文件(看起来其他文件是之前的实验尝试)。

# -*- coding: utf-8 -*-

# Form implementation generated from reading ui file 'ShowEveryone.ui'
#
# Created by: PyQt5 UI code generator 5.5.1
#
# WARNING! All changes made in this file will be lost!

from PyQt5 import QtCore, QtGui, QtWidgets
import sys
import ButtonFunctions
import MainWindow as TheMainWindow

class ShowEveryone(QtWidgets.QMainWindow):
    def __init__(self, parent=None):
        super(ShowEveryone, self).__init__(parent)
        self.setupUi()
        self.setupConnections()

    def setupUi(self):
        self.setObjectName("ShowEveryoneInDatabase")
        self.resize(409, 396)
        icon = QtGui.QIcon()
        icon.addPixmap(QtGui.QPixmap("media/Icon.png"), QtGui.QIcon.Normal, QtGui.QIcon.Off)
        self.setWindowIcon(icon)
        self.setStyleSheet("background-color: qlineargradient(spread:pad, x1:0, y1:0, x2:1, y2:0, stop:0 rgba(9, 41, 4, 255), stop:0.085 rgba(2, 79, 0, 255), stop:0.19 rgba(50, 147, 22, 255), stop:0.275 rgba(236, 191, 49, 255), stop:0.39 rgba(243, 61, 34, 255), stop:0.555 rgba(135, 81, 60, 255), stop:0.667 rgba(121, 75, 255, 255), stop:0.825 rgba(164, 255, 244, 255), stop:0.885 rgba(104, 222, 71, 255), stop:1 rgba(93, 128, 0, 255));")
        self.centralwidget = QtWidgets.QWidget(self)
        self.centralwidget.setObjectName("centralwidget")
        self.gridLayout_2 = QtWidgets.QGridLayout(self.centralwidget)
        self.gridLayout_2.setObjectName("gridLayout_2")
        self.LijstWidget = QtWidgets.QListWidget(self.centralwidget)
        self.LijstWidget.setStyleSheet("color: rgb(0, 0, 0);")
        self.LijstWidget.setObjectName("LijstWidget")
        self.gridLayout_2.addWidget(self.LijstWidget, 1, 0, 1, 1)
        self.ReturnToMainMenuButton = QtWidgets.QCommandLinkButton(self.centralwidget)
        self.ReturnToMainMenuButton.setObjectName("ReturnToMainMenuButton")
        self.gridLayout_2.addWidget(self.ReturnToMainMenuButton, 2, 0, 1, 1)
        self.gridLayout = QtWidgets.QGridLayout()
        self.gridLayout.setObjectName("gridLayout")
        self.IedereenInDatabaseLabel = QtWidgets.QLabel(self.centralwidget)
        self.IedereenInDatabaseLabel.setObjectName("IedereenInDatabaseLabel")
        self.gridLayout.addWidget(self.IedereenInDatabaseLabel, 0, 0, 1, 1)
        self.gridLayout_2.addLayout(self.gridLayout, 0, 0, 1, 1)
        self.setCentralWidget(self.centralwidget)

        self.retranslateUi()
        QtCore.QMetaObject.connectSlotsByName(self)

    def retranslateUi(self):
        _translate = QtCore.QCoreApplication.translate
        self.setWindowTitle(_translate("ShowEveryoneInDatabase", "ToonIedereen - Leewal Database"))
        self.ReturnToMainMenuButton.setText(_translate("ShowEveryoneInDatabase", "Terug naar het hoofdmenu"))
        self.IedereenInDatabaseLabel.setText(_translate("ShowEveryoneInDatabase", "Iedereen in de database:"))

    def setupConnections(self):
        self.ReturnToMainMenuButton.clicked.connect(self.ButtonBackToMainMenuPressed)

    def ButtonBackToMainMenuPressed(self):
        self.close()


def main():
    app = QtWidgets.QApplication(sys.argv)
    w = ShowEveryone()
    w.show()
    sys.exit(app.exec_())

if __name__ == "__main__":
    main()

除了重组之外,崩溃的根本原因是你的插槽:

class ShowEveryone():
    def ButtonBackToMainMenuPressed():
        self.close

没有'ShowEveryone'类的实例。另一个问题是您的成员方法ButtonBackToMainMenuPressed没有self参数。最后,您需要包含括号来调用self.close()

顺便说一下,如果Python崩溃,请包括回溯,例如

python.exe ShowEveryone.py
  File "ShowEveryone.py", line 9
    from PyQt5 import QtCore, QtGui, QtWidgets
    ^
IndentationError: unexpected indent