应用程序结构应该是什么样的?

How application structure should looks like?

我正在尝试创建一个小型数据库应用程序以将所有客户端保存在其中。我想使用 PyQt5 编写 GUI。我对理解应用程序结构的外观有疑问。 我想要一个启动应用程序的主 class,我想在不同的文件中分离 GUI、DB 和 Main class。

您可以在下面看到我的代码片段。它不起作用,因为无法识别某些变量,而且我不明白为什么。

我的想法: 1. Window, tab1对象会在main class init函数中创建 2. 当window, tab1实例被创建时,它的init里面的方法会被调用 3. 我有 window,tab1 对象,它的变量可以自己使用

window.gbT1Main.setLayout(T1LayMain) 未为 TabNewClient class 定义。为什么 ?我应该如何更改我的代码以达到上述要求?请解释我应该如何连接我的 classes :(

Window 和 TabNewClient class(window,tab1)

from PyQt5.QtWidgets import QApplication, QDialog, QTabWidget, QGroupBox, QVBoxLayout, QHBoxLayout, QLabel, QPushButton, QFormLayout, QLineEdit, QDateEdit, QTextEdit, QRadioButton, QGridLayout
import sys
import datetime

class Window(QDialog):
    def __init__(self):
        super().__init__()

        self.InitWindow()

    def InitWindow(self):
        # create tab widget
        self.tab = QTabWidget()

        # create MainWindow groupbox
        self.gbMainWindow = QGroupBox()

        # TAB groupBoxes
        self.gbT1Main = QGroupBox()
        self.gbT2Main = QGroupBox("Main2")
        self.gbT3Main = QGroupBox("Main3")

        # Adding tabs
        self.tab.addTab(self.gbT1Main, "Dodaj klienta")
        self.tab.addTab(self.gbT2Main, "Wyszukaj")
        self.tab.addTab(self.gbT3Main, "Statystki")

        # Setting MainWindow title
        self.setWindowTitle("MEDIKAP - gabinet medycyny pracy")

        # Main Window Layout
        self.layMainWindow = QHBoxLayout()

        # Set MainWindow Layout
        self.layMainWindow.addWidget(self.tab)
        self.gbMainWindow.setLayout(self.layMainWindow)

        # set MainWindow layout visible
        self.setLayout(self.layMainWindow)

        #show window
        self.show()

class TabNewClient:
    def __init__(self):
        self.CreateTab1Layout()

    def CreateTab1Layout(self):

        self.gbAddClient = QGroupBox("Dane klienta")
        self.gbRodzajBadania = QGroupBox("Podstawa prawna")
        self.gbDane = QGroupBox()
        self.gbComment = QGroupBox("Komentarz")
        self.gbButtons = QGroupBox()

        # TAB1 - layouts
        T1LayMain = QVBoxLayout()
        layDane = QHBoxLayout()

        # TAB1
        layDane.addWidget(self.gbAddClient)
        layDane.addWidget(self.gbRodzajBadania)
        self.gbDane.setLayout(layDane)

        # TAB1 - set layout to Main
        T1LayMain.addWidget(self.gbDane)
        T1LayMain.addWidget(self.gbComment)
        T1LayMain.addWidget(self.gbButtons)

        window.gbT1Main.setLayout(T1LayMain)

主要class:


from PyQt5.QtWidgets import QApplication
import sys
from guiv3 import Window, TabNewClient

class Main:
    def __init__(self):

        window = Window()
        tab1 = TabNewClient()

if __name__ == "__main__":
    app = QApplication(sys.argv)

    main = Main()

    app.exec_()

错误: window.gbT1Main.setLayout(T1LayMain) NameError:名称 'window' 未定义

回答为什么会出现关于 'window' not being defined 的错误,我相信这是因为 TabNewClient class 中没有 window 变量.看起来您正在尝试引用 Main 中定义的 window,但这不会起作用,因为该变量不在 TabNewClient [=39= 的范围内].我的猜测是您将 运行 陷入与 gbT1Main 相同的问题,因为这也超出了 TabNewClient class 的范围。

编辑: 我想我理解你在这里的尝试。您希望 window 中的 gbT1Main 相关选项卡保留 tab1 中的布局。为此,您需要在 Main:

中设置 window 的布局
from PyQt5.QtWidgets import QApplication
import sys
from guiv3 import Window, TabNewClient

class Main:
    def __init__(self):

        window = Window()
        tab1 = TabNewClient()
        window.gbT1Main.setLayout(tab1.T1LayMain)


if __name__ == "__main__":
    app = QApplication(sys.argv)

    main = Main()

    app.exec_()

这很可能就是您要找的。

重要:这也要求T1LayMainTabNewClientclass的一个属性,所以在里面使用self.T1LayMain class 以便它可以在其外部访问。