Как из базы данных sqlite3 занести name в QLabel
Это тестовая версия моего окна в приложении и я не знаю как из базы данных занести Name в QLabel
Получаю ошибку:
TypeError: arguments did not match any overloaded call:
from Class_rep import *
from PyQt5.QtWidgets import*
from PyQt5.QtGui import*
from PyQt5.QtCore import*
import sys
import sqlite3
rep = Repository('E:\Запросы польз/orders.db')
class PROFIL_NEW(QMainWindow):
def __init__(self):
super(PROFIL_NEW, self).__init__()
self.setWindowTitle('Профиль')
self.setGeometry(100,100,1000,900)
self.setStyleSheet('.PROFIL_NEW {background-image: url(profil1.jpg);}')
self.profil_instruct()
def profil_instruct(self):
self.photo = QLabel('', self)
self.photo.setGeometry(60,60,320,300)
self.photo.setStyleSheet('.QLabel {background-image: url(photo_profil.jpg);}')
self.lp_name = QLabel('Name:', self)
self.lp_name.setStyleSheet('.QLabel {color: #00b77b}')
self.lp_name.setFont(QFont('Arial', 38, 1, True))
self.lp_name.setGeometry(60,400,300,100)
self.lp_Surname = QLabel('Surname:', self)
self.lp_Surname.setGeometry(60,510,300,100)
self.lp_Surname.setStyleSheet('.QLabel {color: #00b77b;}')
self.lp_Surname.setFont(QFont('Arial', 35, 1, True))
self.lp_Age = QLabel('Age:', self)
self.lp_Age.setGeometry(60,620,300,100)
self.lp_Age.setStyleSheet('.QLabel {color: #00b77b;}')
self.lp_Age.setFont(QFont('Arial', 38, 1, True))
self.lp_Gender = QLabel('Gender:', self)
self.lp_Gender.setGeometry(60,730,300,100)
self.lp_Gender.setStyleSheet('.QLabel {color: #00b77b;}')
self.lp_Gender.setFont(QFont('Arial', 38, 1, True))
self.lp_id = QLabel('id:', self)
self.lp_id.setGeometry(420,50,100,100)
self.lp_id.setStyleSheet('.QLabel {color: #00b77b;}')
self.lp_id.setFont(QFont('Arial', 39, 1, True))
self.lp_login = QLabel('login:', self)
self.lp_login.setGeometry(420,150,200,100)
self.lp_login.setStyleSheet('.QLabel {color: #00b77b;}')
self.lp_login.setFont(QFont('Arial', 37, 1, True))
self.lp_pasword = QLabel('pasword:', self)
self.lp_pasword.setGeometry(420,250,300,100)
self.lp_pasword.setStyleSheet('.QLabel {color: #00b77b;}')
self.lp_pasword.setFont(QFont('Arial', 32, 1, True))
self.s_loginn = input('')
rep.cur.execute("""SELECT name FROM users WHERE login = (?)""", (self.s_loginn,))
name = rep.cur.fetchone()
self.dt_name = QLabel(name, self)
self.dt_name.setStyleSheet('.QLabel {color: #00b77b}')
self.dt_name.setFont(QFont('Arial', 38, 1, True))
self.dt_name.setGeometry(160,400,300,100)
if __name__ == '__main__':
App = QApplication([])
w = PROFIL_NEW()
w.show()
App.exec_()
Ответы (1 шт):
Автор решения: S. Nick
→ Ссылка
Забудьте про input(''), виджет QLineEdit представляет собой однострочный текстовый редактор.
Класс QMainWindow предоставляет главное окно приложения.
...
Примечание. Создание главного окна без центрального виджета не поддерживается. У вас должен быть центральный виджет, даже если это просто заполнитель.
...
и т.д.
import sys
import sqlite3
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.Qt import *
# ??? from Class_rep import *
# ??? rep = Repository('E:\Запросы польз/orders.db')
class ProfilNew(QMainWindow):
def __init__(self):
super(ProfilNew, self).__init__()
self.centralwidget = QtWidgets.QWidget() # !!! +++ важно
self.centralwidget.setObjectName("centralwidget")
self.setCentralWidget(self.centralwidget)
self.gridLayout = QtWidgets.QGridLayout(self.centralwidget)
self.setWindowTitle('Профиль')
self.resize(800, 600)
self.profil_instruct()
self.con = sqlite3.connect("orders.db") # +++
def profil_instruct(self):
self.photo = QLabel()
# self.photo.setStyleSheet('.QLabel {background-image: url(head2.jpg);}')
# +++ установите свое изображенин -------> vvvvvvvvv
self.photo.setPixmap(QtGui.QPixmap("head2.jpg").scaled(200, 200)) # +++
self.gridLayout.addWidget(self.photo, 0, 0, 3, 2, alignment = Qt.AlignCenter)
self.lp_name = QLabel('Name:', self)
self.gridLayout.addWidget(self.lp_name, 3, 0, 1, 1)
self.lp_Surname = QLabel('Surname:', self)
self.gridLayout.addWidget(self.lp_Surname, 4, 0, 1, 1)
self.lp_Age = QLabel('Age:', self)
self.gridLayout.addWidget(self.lp_Age, 5, 0, 1, 1)
self.lp_Gender = QLabel('Gender:', self)
self.gridLayout.addWidget(self.lp_Gender, 6, 0, 1, 1)
self.lp_id = QLabel('id:', self)
self.gridLayout.addWidget(self.lp_id, 0, 2, 1, 1)
self.lp_login = QLabel('login:', self)
self.gridLayout.addWidget(self.lp_login, 1, 2, 1, 1)
self.lp_pasword = QLabel('pasword:', self)
self.gridLayout.addWidget(self.lp_pasword, 2, 2, 1, 1)
self.lineEdit = QLineEdit(placeholderText='Введите логин для поиска') # +++
self.gridLayout.addWidget(self.lineEdit, 7, 0, 1, 2)
self.pushButton = QPushButton('Поиск') # +++
self.pushButton.clicked.connect(self.search)
self.pushButton.setMinimumWidth(400)
self.gridLayout.addWidget(self.pushButton, 7, 2, 1, 2)
self.dt_name = QLabel()
self.gridLayout.addWidget(self.dt_name, 3, 1, 1, 1)
def search(self): # +++
s_loginn = self.lineEdit.text()
if not s_loginn:
msg = QMessageBox.information(self,
'Внимание',
"Введите логин для поиска."
)
return
cur = self.con.cursor()
rep = cur.execute("""SELECT name FROM users WHERE login = (?)""", (s_loginn,))
rep = rep.fetchone()
if rep:
name = rep[0]
self.dt_name.setText(name)
else:
msg = QMessageBox.information(self,
'Внимание',
f"Нет записи для логина {s_loginn}."
)
# тут ваши стили
qss = '''
#centralwidget {
/* установите свое изображенин --> vvvvvvvvv */
background-image: url(first.jpg);
}
QLabel {
color: #00b77b;
}
QMessageBox {
color: #ffb77b;
}
QMessageBox QPushButton {
font-size: 12px;
}
QMessageBox QLabel {
color: #ff5555;
font-size: 18px;
}
'''
if __name__ == '__main__':
app = QApplication(sys.argv)
app.setFont(QtGui.QFont('Arial', 32, 1, True)) # +++
app.setStyleSheet(qss) # +++
w = ProfilNew()
w.show()
sys.exit(app.exec())




