I'm writing a GUI application and I'm having some problems. I am building this app with the help of chatgpt but I have got this error and don't know how to fix it. When I open the app and I touch the combobox I can see the options which are the cities to be choosed. When I save the workorder and want to create a new one without closing the app the QComboBox doesn't work. Meanwhile the order_id when I want to create a new workorder it doesn't appear in the inputfield but it gets saved in the database correctly and gets the last Id. How to fix these issues? I tried to create a new function def new_work_order but that didn't help as well or maybe I was doing something wrong. Here is my code:
from PyQt5 import QtWidgets, QtGui
import sys
from PyQt5.QtWidgets import QApplication, QWidget, QLabel, QPushButton, QLineEdit, QVBoxLayout, QDateEdit, QComboBox, QStyleFactory, QTableView, QMessageBox
from PyQt5.QtCore import Qt
from PyQt5.QtGui import QFont, QStandardItemModel, QStandardItem
import psycopg2
import datetime
from PyQt5.QtCore import QDate
from PyQt5.QtWidgets import QDateEdit
from PyQt5.QtCore import QTimer
current_date = QDate.currentDate()
# Connect to the database
conn = psycopg2.connect(
host="localhost",
database="mms",
user="postgres",
password="postgres"
)
# Create a cursor object
cur = conn.cursor()
# Check if the `maintenance` table exists
cur.execute("SELECT * FROM information_schema.tables WHERE table_name='t_wo_workorders'")
if not cur.fetchone():
# Create the `maintenance` table
cur.execute(
"CREATE TABLE t_wo_workorders (order_id integer Primary Key, defined DATE, org VARCHAR(255), order_type VARCHAR(255), scheduled DATE, status VARCHAR(255), request VARCHAR(255), address VARCHAR(255), customer VARCHAR(255), tel_no VARCHAR(255))"
)
conn.commit()
class MaintenanceManagementSystem(QWidget):
def __init__(self):
super().__init__()
self.org_model = QStandardItemModel()
self.org_model.appendRow(QStandardItem("Prizren"))
self.org_model.appendRow(QStandardItem("Suharekë"))
self.org_model.appendRow(QStandardItem("Malishevë"))
self.org_model.appendRow(QStandardItem("Dragash"))
self.org_input = QComboBox()
self.org_input.setModel(self.org_model)
cur = conn.cursor()
cur.execute("SELECT MAX(order_id) FROM t_wo_workorders")
last_saved_id = cur.fetchone()[0]
self.last_id = 1
self.last_id = last_saved_id or 0
self.counter = self.last_id
self.counter += 1
org = self.org_input.currentText()
save_button = QPushButton('Save')
save_button.clicked.connect(self.save_work_order)
# Set the window title and size
self.setWindowTitle('Maintenance Management System')
self.resize(600, 300)
# Create a label and set its font and alignment
label = QLabel('Maintenance Management System')
label.setFont(QFont('Arial', 20))
label.setAlignment(Qt.AlignCenter)
# Create labels and input fields
order_id_label = QLabel('Order ID')
self.order_id_input = QLineEdit()
self.order_id_input.setReadOnly(True)
self.counter = self.last_id
self.counter += 1
self.generate_order_id()
defined_label = QLabel('Defined')
self.defined_input = QDateEdit(current_date)
self.defined_input.setCalendarPopup(True)
org_label = QLabel('Org. Unit')
order_type_label = QLabel('Order Type')
self.order_type_input = QLineEdit()
scheduled_label = QLabel('Scheduled')
self.scheduled_input = QDateEdit(current_date)
self.scheduled_input.setCalendarPopup(True)
status_label = QLabel('Status')
self.status_input = QLineEdit()
request_label = QLabel('Request')
self.request_input = QLineEdit()
address_label = QLabel('Address')
self.address_input = QLineEdit()
customer_label = QLabel('Customer')
self.customer_input = QLineEdit()
tel_no_label = QLabel('Tel Number')
self.tel_no_input = QLineEdit()
self.table_view = QTableView()
# Set the size of the input fields
self.order_id_input.setFixedWidth(100)
self.defined_input.setFixedWidth(100)
self.org_input.setFixedWidth(100)
self.order_type_input.setFixedWidth(100)
self.scheduled_input.setFixedWidth(100)
self.status_input.setFixedWidth(100)
self.request_input.setFixedWidth(100)
self.address_input.setFixedWidth(100)
self.customer_input.setFixedWidth(100)
self.tel_no_input.setFixedWidth(100)
# Create a vertical box layout and add it to the main window
layout = QVBoxLayout()
layout.addWidget(label)
self.setLayout(layout)
# Add the label, button, and input fields
layout.addWidget(order_id_label)
layout.addWidget(self.order_id_input)
layout.addWidget(defined_label)
layout.addWidget(self.defined_input)
layout.addWidget(org_label)
layout.addWidget(self.org_input)
layout.addWidget(order_type_label)
layout.addWidget(self.order_type_input)
layout.addWidget(order_type_label)
layout.addWidget(self.order_type_input)
layout.addWidget(scheduled_label)
layout.addWidget(self.scheduled_input)
layout.addWidget(status_label)
layout.addWidget(self.status_input)
layout.addWidget(request_label)
layout.addWidget(self.request_input)
layout.addWidget(address_label)
layout.addWidget(self.address_input)
layout.addWidget(customer_label)
layout.addWidget(self.customer_input)
layout.addWidget(tel_no_label)
layout.addWidget(self.tel_no_input)
# Add the save button to the layout
layout.addWidget(save_button)
save_button.setStyleSheet("QPushButton:pressed { background-color: grey; }")
layout.addWidget(save_button)
# Create the `maintenance` table
def reset_input_fields(self):
self.order_id_input.setText("")
self.defined_input.setDate(current_date)
self.org_input.setCurrentIndex(0)
self.order_type_input.setText("")
self.scheduled_input.setDate(current_date)
self.status_input.setText("")
self.request_input.setText("")
self.address_input.setText("")
self.customer_input.setText("")
self.tel_no_input.setText("")
def update_table_view(self):
cur = conn.cursor()
cur.execute("SELECT * FROM t_wo_workorders")
rows = cur.fetchall()
model = QStandardItemModel()
model.setHorizontalHeaderLabels(['order_id', 'defined', 'org', 'order_type', 'scheduled', 'status', 'request', 'address', 'customer', 'tel_no'])
for row in rows:
items = [QStandardItem(str(cell)) for cell in row]
model.appendRow(items)
self.table_view.setModel(model)
def generate_order_id(self):
self.last_id += 1
self.order_id_input.setText(str(self.counter))
def save_work_order(self):
# Get the values entered by the user
order_id = self.generate_order_id()
defined = self.defined_input.date().toString("yyyy-MM-dd")
org = self.org_input.currentText()
order_type = self.order_type_input.text()
scheduled = self.scheduled_input.date().toString("yyyy-MM-dd")
status = self.status_input.text()
request = self.request_input.text()
address = self.address_input.text()
customer = self.customer_input.text()
tel_no = self.tel_no_input.text()
self.order_id_input.setText(str(self.counter))
# Insert the data into the `maintenance` table
cur.execute(
"INSERT INTO t_wo_workorders (order_id, defined, org, order_type, scheduled, status, request, address, customer, tel_no) VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s)",
(self.order_id_input.text(), defined, self.org_input.currentText(), self.order_type_input.text(), scheduled, self.status_input.text(), self.request_input.text(), self.address_input.text(), self.customer_input.text(), self.tel_no_input.text()),
)
conn.commit()
self.update_table_view()
# Create a new standard item model
model = QStandardItemModel()
# Set the horizontal header labels
model.setHorizontalHeaderLabels(["Order ID", "Defined", "Org. Unit", "Order Type", "Scheduled", "Status", "Request", "Address", "Customer", "Tel Number"])
# Define a list of rows, where each row is a list of cell values
data = [
[order_id, defined, org, order_type, scheduled, status, request, address, customer, tel_no]
]
# Iterate over the rows
for i, row in enumerate(data):
# Iterate over the cell values in the row
for j, value in enumerate(row):
# Set the value of the cell
model.setItem(i, j, QStandardItem(value))
# Set the model for the table view
self.table_view.setModel(model)
# Clear the input fields
self.order_id_input.clear()
self.defined_input.clear()
self.org_input.clear()
self.order_type_input.clear()
self.scheduled_input.clear()
self.status_input.clear()
self.request_input.clear()
self.address_input.clear()
self.customer_input.clear()
self.tel_no_input.clear()
# Generate a new order ID
self.generate_order_id()
response = QMessageBox.question(self, "Message", "Work-order with ID: <font color='red'>" + str(self.counter) + "</font> was saved successfully. Do you want to create another work order?", QMessageBox.Yes | QMessageBox.No, QMessageBox.No)
# response.setStyleSheet("color: red;")
if response == QMessageBox.Yes:
self.counter += 1
self.generate_order_id()
self.reset_input_fields()
else:
self.close()
# Create an instance of QApplication
app = QApplication(sys.argv)
# Create an instance of your application
mms = MaintenanceManagementSystem()
# Load the stylesheet
with open('styles.css', 'r') as f:
stylesheet = f.read()
# Apply the stylesheet to the application
app.setStyleSheet(stylesheet)
# Show the application window
mms.show()
# Run the application
sys.exit(app.exec_())
The reason your combobox isn't working is because you are clearing it when you reset the order forms inputs.
To solve that simply remove the line self.org_input.clear()
from your code.
The reason that your order_id doesn't match is because you use two different methods to get the information.
When you save the record to the database you are using self.order_id_input.text()
, but when you get the same value for your table you use self.generate_order_id()
, which doesn't have a return value. So that means your order_id is being set to None
. So to fix it just use the same method for both use cases.
For example:
def save_work_order(self):
# Get the values entered by the user
order_id = self.order_id_input.text() # <----- changed this
...
...