Need some help
-
Hi all
I have a conception with a technical follow up question around PyQt5, Python 3 and Postgres.
I have a Postgres DB setup and populated with tables and information. Now my aim is to get my front end ( in Pyqt5 ) to talk to the DB and retrieve data. Now in Python, I know how to do that ( not displayed in the code below ).
My question is that if you run the main code you will get the GUI pop up. ( I can't upload a picture )
What needs to happen is that the player must be able to enter data that writes back to the DB and I want to add drop-down menus that populate with the information inside the DB.
Form an architecture persp0ective I want to know if I am moving in the right direction?
From a coding perspective how the hell do I link the text field to save back to the DB?
I know this is a lot of questions but I could really need some pro's insight
import sys
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
from PyQt5 import *class Window(QMainWindow):
def init(self):
super().init()self.title = "New Player Screen" self.left = 100 self.top = 100 self.width = 700 self.height = 540 self.setWindowIcon(QtGui.QIcon("D:\icon.png")) self.textbox = "User input" name = QtWidgets.QLabel(self) name.setText("Character Name :") name.setStyleSheet("font: 10pt CODE") name.setGeometry(125,150,120,130) self.textbox = QLineEdit(self) self.textbox.move(230,207) self.textbox.resize(200,20) close_button = QPushButton(" Close ",self) close_button.move(490,450) close_button.setToolTip("<h3>This is my click button") close_button.clicked.connect(self.close) self.InitUI() def InitUI(self): self.setWindowTitle(self.title) self.setGeometry(self.left, self.top,self.width,self.height) self.show() def Close(self): QCoreApplication.instance().quit()
App = QApplication(sys.argv)
window = Window()
sys.exit(App.exec()) -
First off regardless of the coding language you should follow the MVC (Model-View-Controller) methodology of coding which basically divorces the front-end (View) from the back-end (Model) by putting in a middle interpreter layer (Controller). Why do this well first it has the benefit of separating functionality and focusing each element on what it does best. The GUI (View) is best for handling the user interface and not very good with dealing with the data, while the Database (Model) is best for handling the data and is not very good with handling the user interface. The Controller is just the method for moving the data from the View to Model. This also has a secondary benefit if you ever change the back-end with a different database engine the only thing that is effected is the Model and maybe a bit of the Controller the GUI is essentially untouched and vice-versa if you wish to change the front-end the only thing that really changes is the front-end since all the API calls to the middle-tier would essentially remain the same. Further should the business logic affecting the data occur that is generally dealt with within the Controller or the Database depending on the implementation of the business logic. For instance data validation and manipulation generally takes place within the Controller layer making sure that no bad-data gets into the database which is always a nightmare and something that happens often if you directly tie the front-end into the back-end.
So in short use pyqt5 to handle your front-end GUI and use python to handle your interface to your database (validating and moving the data to the database) and let the database do what its best at doing - handling the data with storage and stored-procedures, triggers, etc...
I hope that helps