- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我的问题是:如何更改标签中的文本?标签位于布局内,但 setText()
似乎不起作用 - 也许我没有做对。
这是我的代码:
这是主窗口 GUI,Qt-Designer 提供的那个:
class Ui_dashboard(QtGui.QMainWindow):
def setupUi(self, dashboard):
dashboard.setObjectName(_fromUtf8("dashboard"))
dashboard.resize(734, 703)
icon = QtGui.QIcon()
icon.addPixmap(QtGui.QPixmap(_fromUtf8("../../../../Documents and Settings/cgarcia/Escritorio/metromara - imagenes - nerio/logoM.JPG")), QtGui.QIcon.Normal, QtGui.QIcon.Off)
dashboard.setWindowIcon(icon)
dashboard.setWindowOpacity(1.0)
self.centralwidget = QtGui.QWidget(dashboard)
self.centralwidget.setObjectName(_fromUtf8("centralwidget"))
self.frame = QtGui.QFrame(self.centralwidget)
self.frame.setGeometry(QtCore.QRect(30, 20, 261, 119))
self.frame.setFrameShape(QtGui.QFrame.Box)
self.frame.setFrameShadow(QtGui.QFrame.Plain)
self.frame.setObjectName(_fromUtf8("frame"))
self.gridLayout = QtGui.QGridLayout(self.frame)
self.gridLayout.setObjectName(_fromUtf8("gridLayout"))
self.label_5 = QtGui.QLabel(self.frame)
font = QtGui.QFont()
font.setBold(True)
font.setWeight(75)
self.label_5.setFont(font)
self.label_5.setObjectName(_fromUtf8("label_5"))
self.gridLayout.addWidget(self.label_5, 4, 0, 1, 1)
self.label_4 = QtGui.QLabel(self.frame)
font = QtGui.QFont()
font.setBold(True)
font.setWeight(75)
self.label_4.setFont(font)
self.label_4.setObjectName(_fromUtf8("label_4"))
self.gridLayout.addWidget(self.label_4, 3, 0, 1, 1)
self.label_ci = QtGui.QLabel(self.frame)
font = QtGui.QFont()
font.setBold(True)
font.setWeight(75)
self.label_ci.setFont(font)
self.label_ci.setObjectName(_fromUtf8("label_ci"))
self.gridLayout.addWidget(self.label_ci, 1, 1, 1, 1)
self.label_cargo = QtGui.QLabel(self.frame)
font = QtGui.QFont()
font.setBold(True)
font.setWeight(75)
self.label_cargo.setFont(font)
self.label_cargo.setObjectName(_fromUtf8("label_cargo"))
self.gridLayout.addWidget(self.label_cargo, 2, 1, 1, 1)
self.label_gerencia = QtGui.QLabel(self.frame)
font = QtGui.QFont()
font.setBold(True)
font.setWeight(75)
self.label_gerencia.setFont(font)
self.label_gerencia.setObjectName(_fromUtf8("label_gerencia"))
self.gridLayout.addWidget(self.label_gerencia, 3, 1, 1, 1)
self.label_nombre = QtGui.QLabel(self.frame)
font = QtGui.QFont()
font.setBold(True)
font.setWeight(75)
self.label_nombre.setFont(font)
self.label_nombre.setObjectName(_fromUtf8("label_nombre"))
self.gridLayout.addWidget(self.label_nombre, 0, 1, 1, 1)
self.label_2 = QtGui.QLabel(self.frame)
font = QtGui.QFont()
font.setBold(True)
font.setWeight(75)
self.label_2.setFont(font)
self.label_2.setObjectName(_fromUtf8("label_2"))
self.gridLayout.addWidget(self.label_2, 1, 0, 1, 1)
self.label = QtGui.QLabel(self.frame)
font = QtGui.QFont()
font.setBold(True)
font.setWeight(75)
self.label.setFont(font)
self.label.setObjectName(_fromUtf8("label"))
self.gridLayout.addWidget(self.label, 0, 0, 1, 1)
self.label_3 = QtGui.QLabel(self.frame)
font = QtGui.QFont()
font.setBold(True)
font.setWeight(75)
self.label_3.setFont(font)
self.label_3.setObjectName(_fromUtf8("label_3"))
self.gridLayout.addWidget(self.label_3, 2, 0, 1, 1)
self.label_departamento = QtGui.QLabel(self.frame)
font = QtGui.QFont()
font.setBold(True)
font.setWeight(75)
self.label_departamento.setFont(font)
self.label_departamento.setObjectName(_fromUtf8("label_departamento"))
self.gridLayout.addWidget(self.label_departamento, 4, 1, 1, 1)
self.treeView = QtGui.QTreeView(self.centralwidget)
self.treeView.setGeometry(QtCore.QRect(30, 160, 161, 481))
self.treeView.setFrameShape(QtGui.QFrame.Box)
self.treeView.setFrameShadow(QtGui.QFrame.Plain)
self.treeView.setObjectName(_fromUtf8("treeView"))
dashboard.setCentralWidget(self.centralwidget)
self.menubar = QtGui.QMenuBar(dashboard)
self.menubar.setGeometry(QtCore.QRect(0, 0, 734, 20))
self.menubar.setObjectName(_fromUtf8("menubar"))
dashboard.setMenuBar(self.menubar)
self.statusbar = QtGui.QStatusBar(dashboard)
self.statusbar.setObjectName(_fromUtf8("statusbar"))
dashboard.setStatusBar(self.statusbar)
self.retranslateUi(dashboard)
QtCore.QMetaObject.connectSlotsByName(dashboard)
def retranslateUi(self, dashboard):
dashboard.setWindowTitle(_translate("dashboard", "Metro de Maracaibo.", None))
self.label_5.setText(_translate("dashboard", "DEPARTAMENTO", None))
self.label_4.setText(_translate("dashboard", "GERENCIA", None))
self.label_ci.setText(_translate("dashboard", "...", None))
self.label_cargo.setText(_translate("dashboard", "...", None))
self.label_gerencia.setText(_translate("dashboard", "...", None))
self.label_nombre.setText(_translate("dashboard", "...", None))
self.label_2.setText(_translate("dashboard", "C.I.:", None))
self.label.setText(_translate("dashboard", "NOMBRE", None))
self.label_3.setText(_translate("dashboard", "CARGO", None))
self.label_departamento.setText(_translate("dashboard", "...", None))
这是接口(interface)的逻辑:
class logica_login(Ui_Dialog):
def __init__(self):
QtGui.QDialog.__init__(self)
#QtGui.QMainWindow.__init__(self)
Dialog = Ui_Dialog(self)
self.setupUi(Dialog)
self.bloguin_aceptar.clicked.connect(self.validacion)
self.blogin_cancelar.clicked.connect(self.reject)
Dialog.show()
Dialog.exec_()
#PRINT DE PRUEBA
print('hola')
#METODO DE VALIDACION
#####################
def validacion(self):
#CONEXION A LA BASE DE DATOS.
#############################
db = QSqlDatabase.addDatabase("QPSQL")
db.setHostName("localhost")
db.setDatabaseName("postgres")
db.setUserName("postgres")
db.setPassword("1234")
#self.ui.label_nombre.setText(str(Ui_Dialog.le_user.text()))
#RESPUESTA EN CASO DE FALLO EN CONEXION CON LA BASE DE DATOS.
#############################################################
if not db.open():
QtGui.QMessageBox.about(self, "ERROR","LO SENTIMOS, EXPERIMENTAMOS PROBLEMAS DE CONEXION CON EL SERVIDOR.")
#CONSULTA A LA BASE DE DATOS DE LA CLAVE Y CONTRASEÑA
#####################################################
consulta = QSqlQueryModel()
consulta.setQuery("SELECT usuario, contraseña, id FROM usuarios")
lista = []
i = consulta.rowCount()
#BUCLE DE EXTRACCION Y REORDENACION DE DATOS EN LA LISTA "LISTA"
################################################################
while i >= 0:
db_user = consulta.record(i).value("usuario");
db_pass = consulta.record(i).value("contraseña");
db_id = consulta.record(i).value("id")
str_user = str(db_user)
str_pass = str(db_pass)
auth_db = (str_user, str_pass)
lista.append(auth_db)
i = (i-1)
print(lista)
user = self.le_user.text()
passw = self.le_pass.text()
for usrname, password in lista:
if ((user == usrname) and (passw == password)):
print('logueo exitoso')
QtGui.QMessageBox.about(self, "Logue Exitoso.", "ACCESO CONCEDIDO.")
self.deleteLater()
go = True
print(go)
dashboard.showMaximized()
#self.ui.label_nombre.setText(str(Ui_Dialog.le_user.text())) ### LOGICA_LOGIN NO TIENE ATRIBUTO UI
break;
elif ((user == usrname) and (passw != password)):
QtGui.QMessageBox.about(self, "ERROR","Contraseña Incorrecta.")
break;
elif ((user != usrname) and (passw == password)):
QtGui.QMessageBox.about(self, "ERROR","Nombre de usuario Incorrecto")
break;
else:
QtGui.QMessageBox.about(self, "ERROR","Nombre de usuario y Contraseña Incorrecto")
class logica_tablero(Ui_dashboard):
def __init__(self):
l_n = Ui_dashboard.label_nombre()
QtGui.QMainWindow.__init__(self)
self.ui = Ui_dashboard(self)
self.ui.setupUi(self)
#logica_tablero.ui.l_n.setText("hola")
Ui_dashboard.label_5.setText(("hola"))
def main():
app = QtGui.QApplication(sys.argv)
if 1 == 1 :
loginApp = logica_login()
loginApp.show()
else:
mainApp = Ui_dashboard()
mainApp.setupUi(dash)
dash.showMaximized()
#Ui_dashboard.label_nombre.setText(("hola"))
sys.exit(app.exec_())
if __name__ == "__main__":
app = QtGui.QApplication(sys.argv)
## ESTA PARTE HA SIDO UN DOLOR DE CABEZA, NO OLVIDAR NI BORRAR.
#BUCLE DE DASHBOARD
#---------------------------------
dashboard = QtGui.QMainWindow()
mainApp = Ui_dashboard()
mainApp.setupUi(dashboard)
#BUCLE DE LOGIN
#---------------------------------
l = logica_login()
l
#---------------------------------
sys.exit(app.exec_())
我想在用户登录程序后更改标签。我要更改的标签是 label_nombre
,我想将其替换为 le_user
,它对应于其中一个 QLineEdits。
更新
我做了您告诉我要做的所有更改,该项目看起来组织得很好,但现在我得到了这个输出:
TypeError:描述符“init”需要一个“sip.simplewrapper”对象但收到一个“logica_login”
更新 2
*TypeError:描述符“init”需要一个“sip.simplewrapper”对象但收到了一个“logica_login”-------------------- ------> 解决
现在,我遇到了开始所有交易时遇到的同样问题,标签不会更改其内容。
最佳答案
您没有正确使用 pyuic 创建的模块。您应该永远不要直接编辑这些模块 - 它们应该导入到您的主应用程序中。
pyuic 生成的 UI 类有一个 setupUi
方法。此方法采用您在 Qt Designer 中创建的顶级类的实例,并将从设计器中添加所有小部件到该实例。例如,label_nombre
将成为传递给 setupUi
的实例的一个属性。通常,您会创建顶级类的子类,然后将 self
作为实例传入(见下文)。
我建议您使用 pyuic 重新生成您的 ui 文件并将它们另存为,比如 dialog_ui.py
和 dashboard_ui.py
。
你的程序结构会变成这样:
from PyQt4 import QtCore, QtGui
from dashboard_ui import Ui_dashboard
from dialog_ui import Ui_Dialog
class logica_login(QtGui.QDialog, Ui_Dialog):
def __init__(self):
QtGui.QDialog.__init__(self)
self.setupUi(self)
self.bloguin_aceptar.clicked.connect(self.validacion)
self.blogin_cancelar.clicked.connect(self.reject)
def validacion(self):
...
class logica_tablero(QtGui.QMainWindow, Ui_dashboard):
def __init__(self):
QtGui.QMainWindow.__init__(self)
self.setupUi(self)
self.label_nombre.setText("hola")
...
if __name__ == "__main__":
app = QtGui.QApplication(sys.argv)
login = logica_login()
if login.exec_() == QtGui.QDialog.Accepted:
dashboard = logica_tablero()
dashboard.showMaximized()
sys.exit(app.exec_())
关于python - 在 PyQt4 中动态更改 QLabel 文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19985640/
#include #include int main(int argc, char** argv) { QApplication app(argc, argv); QLabel l
我有一个 QLabel,我想根据它包含的文本(加上侧面的一些边距)调整它的大小。,我已经尝试过: self.WarnLab = QtGui.QLabel(self.HeaderRight) font
我试图从一个函数返回一个 QLabel,但我一直收到错误: /media/root/5431214957EBF5D7/projects/c/qt/tools/plugandpaint/plugins/
我看过很多关于如何使用 paintevent 的示例,但我就是无法让它工作。 我的 .ui 文件中有一个名为“图像”的标签,我正试图在其中绘画。我失败得很惨。在我见过的大多数示例中,他们都使用了 QL
我试图让 QLabel 显示图像和一些与该图像垂直居中的文本。我不知道该怎么做。我在互联网上找到的大多数资源都建议使用以下代码: ui->label->setText(" Hello"); 但是我得到
我正在尝试使用 PyQt 为一段 python 代码制作一个 GUI。但是,由于我不知道的原因,GUI 似乎切断了两个 Qlabel 的末端。我已经针对类似问题浏览了 Stack Overflow 上
我正在尝试在 QLabel 中显示一些 html 代码。虽然 QLabel 正确呈现 html,但超链接实际上不起作用,图像链接只是生成丢失的图标图片而不是显示图像本身。 我猜这是 QLabel 的一
我有一个用样式表填充红色的 QLabel,但 QLabel 是矩形的,我想要一个圆形。我尝试添加边框半径,但它不起作用,可能是因为我将 QLabel 放在了 formLayout 中。 有没有一种简单
我正在实例化一个可编辑的 QLabel,如下所示: QLabel foo("some text"); foo.setTextInteractionFlags(Qt::TextEditorInterac
我想在 QT/PySide2 应用程序中显示圆形图像。 下面是我试过的代码。 self.statusWidget = QLabel() img = QImage(":/image.jpg").scal
如何设置QLabel的文本颜色和背景? 最佳答案 最好和推荐的方法是使用 Qt 样式表。文件:Qt 5 Style Sheet , Qt 6 Style Sheet . 要更改 QLabel 的文本颜
我正在尝试创建带有文本轮廓的标签。我只想要一个带有黑色轮廓的简单白色文本。我首先尝试在 css 中这样做 label.setStyleSheet("color:white; outline:2px b
我正在尝试创建带有文本轮廓的标签。我只想要一个带有黑色轮廓的简单白色文本。我首先尝试在 css 中这样做 label.setStyleSheet("color:white; outline:2px b
我读过这个How to distinguish between mouseReleaseEvent and mousedoubleClickEvent on QGrapnhicsScene还有这个Di
我正在尝试制作一个转盘播放器,当按住鼠标左键并向左或向右拖动时,它可以翻转图像序列。它几乎可以工作并且正在打印出正确的图像名称。但图像本身不会更新/重新绘制。 如果我从 eventFilter 方法的
我有一个这样的段落列表: list1 = [ "something", "more", ... "{ //image// }" "something" ... "{ //i
我自己设置 PyQt 时遇到问题。我的想法是创建一个带有歌曲标题和专辑封面的音乐播放器。我已经成功创建了自己的窗口并添加了专辑封面。但我无法将标签添加到正确的位置。我希望歌曲标题位于窗口的顶部中心,如
当我运行以下代码时,label 小部件 (?) 会显示,即使它尚未添加到任何布局中。我正在遵循的教科书还暗示我不应该看到它(直到我将其添加到布局中),但它无论如何都会出现。我期待看到一个空 windo
我试图在不使用 setScaledContents(True) 的情况下获得完全适合我的标签的图像,因为我希望 ImageGrab 具有与 QLabel 空间完全相同的尺寸。 我使用带有 bbox 的
我有几个 QLabel 实例,我希望这些实例没有边框或背景颜色。我尝试了以下方法: plbl = new QLabel(); plbl->setGeometry(210, 0, 26, 16); pl
我是一名优秀的程序员,十分优秀!