gpt4 book ai didi

python - 布局在执行 pyqt 时不起作用

转载 作者:太空宇宙 更新时间:2023-11-03 14:10:30 24 4
gpt4 key购买 nike

在 pyqt4 设计器中,我创建了 UI 并设置了布局,这是我的代码生成的

pyuic4 

在Python中:

# -*- coding: utf-8 -*-
from PyQt4 import QtCore, QtGui

try:
_fromUtf8 = QtCore.QString.fromUtf8
except AttributeError:
def _fromUtf8(s):
return s

try:
_encoding = QtGui.QApplication.UnicodeUTF8
def _translate(context, text, disambig):
return QtGui.QApplication.translate(context, text, disambig,
_encoding)
except AttributeError:
def _translate(context, text, disambig):
return QtGui.QApplication.translate(context, text, disambig)

class Ui_Dialog(object):
def setupUi(self, Dialog):
Dialog.setObjectName(_fromUtf8("Dialog"))
Dialog.resize(354, 304)
self.gridLayout_2 = QtGui.QGridLayout(Dialog)
self.gridLayout_2.setObjectName(_fromUtf8("gridLayout_2"))
self.verticalLayout = QtGui.QVBoxLayout()
self.verticalLayout.setObjectName(_fromUtf8("verticalLayout"))
self.label_6 = QtGui.QLabel(Dialog)
self.label_6.setObjectName(_fromUtf8("label_6"))
self.verticalLayout.addWidget(self.label_6)
self.gridLayout_2.addLayout(self.verticalLayout, 0, 0, 1, 2)
self.horizontalLayout = QtGui.QHBoxLayout()

self.horizontalLayout.setObjectName(_fromUtf8("horizontalLayout"))
self.label = QtGui.QLabel(Dialog)
self.label.setEnabled(True)
self.label.setObjectName(_fromUtf8("label"))
self.horizontalLayout.addWidget(self.label)
self.line_nume = QtGui.QLineEdit(Dialog)
self.line_nume.setObjectName(_fromUtf8("line_nume"))
self.horizontalLayout.addWidget(self.line_nume)
self.gridLayout_2.addLayout(self.horizontalLayout, 1, 0, 1, 2)
self.horizontalLayout_2 = QtGui.QHBoxLayout()

self.horizontalLayout_2.setObjectName(_fromUtf8("horizontalLayout_2"))
self.label_2 = QtGui.QLabel(Dialog)
self.label_2.setObjectName(_fromUtf8("label_2"))
self.horizontalLayout_2.addWidget(self.label_2)
self.line_prenume = QtGui.QLineEdit(Dialog)
self.line_prenume.setObjectName(_fromUtf8("line_prenume"))
self.horizontalLayout_2.addWidget(self.line_prenume)
self.gridLayout_2.addLayout(self.horizontalLayout_2, 2, 0, 1, 2)
self.gridLayout = QtGui.QGridLayout()
self.gridLayout.setSizeConstraint(QtGui.QLayout.SetMaximumSize)
self.gridLayout.setObjectName(_fromUtf8("gridLayout"))
self.F = QtGui.QRadioButton(Dialog)
self.F.setObjectName(_fromUtf8("F"))
self.gridLayout.addWidget(self.F, 1, 1, 1, 1)
self.M = QtGui.QRadioButton(Dialog)
self.M.setObjectName(_fromUtf8("M"))
self.gridLayout.addWidget(self.M, 0, 1, 1, 1)
self.label_3 = QtGui.QLabel(Dialog)
self.label_3.setObjectName(_fromUtf8("label_3"))
self.gridLayout.addWidget(self.label_3, 0, 0, 1, 1)
self.gridLayout_2.addLayout(self.gridLayout, 3, 0, 1, 1)
self.verticalLayout_2 = QtGui.QVBoxLayout()
self.verticalLayout_2.setObjectName(_fromUtf8("verticalLayout_2"))
self.gridLayout_2.addLayout(self.verticalLayout_2, 3, 1, 2, 1)
self.horizontalLayout_3 = QtGui.QHBoxLayout()


self.horizontalLayout_3.
setSizeConstraint(QtGui.QLayout.SetMaximumSize)


self.horizontalLayout_3.setObjectName(_fromUtf8("horizontalLayout_3"))
self.label_4 = QtGui.QLabel(Dialog)
self.label_4.setObjectName(_fromUtf8("label_4"))
self.horizontalLayout_3.addWidget(self.label_4)
self.spin = QtGui.QSpinBox(Dialog)
sizePolicy = QtGui.QSizePolicy(QtGui.QSizePolicy.Fixed,
QtGui.QSizePolicy.Fixed)
sizePolicy.setHorizontalStretch(0)
sizePolicy.setVerticalStretch(0)

sizePolicy.setHeightForWidth(self.spin.sizePolicy()
.hasHeightForWidth())
self.spin.setSizePolicy(sizePolicy)
self.spin.setMinimum(13)
self.spin.setObjectName(_fromUtf8("spin"))
self.horizontalLayout_3.addWidget(self.spin)
self.gridLayout_2.addLayout(self.horizontalLayout_3, 4, 0, 1, 1)
self.horizontalLayout_4 = QtGui.QHBoxLayout()

self.horizontalLayout_4.setObjectName(_fromUtf8("horizontalLayout_4"))
self.label_5 = QtGui.QLabel(Dialog)
self.label_5.setObjectName(_fromUtf8("label_5"))
self.horizontalLayout_4.addWidget(self.label_5)
self.line_mail = QtGui.QLineEdit(Dialog)
self.line_mail.setObjectName(_fromUtf8("line_mail"))
self.horizontalLayout_4.addWidget(self.line_mail)
self.gridLayout_2.addLayout(self.horizontalLayout_4, 5, 0, 1, 2)
self.OK = QtGui.QPushButton(Dialog)
self.OK.setMaximumSize(QtCore.QSize(85, 27))
self.OK.setObjectName(_fromUtf8("OK"))
self.gridLayout_2.addWidget(self.OK, 6, 1, 1, 1)

self.retranslateUi(Dialog)
QtCore.QMetaObject.connectSlotsByName(Dialog)

def retranslateUi(self, Dialog):
Dialog.setWindowTitle(_translate("Dialog", "Dialog", None))
self.label_6.setText(_translate("Dialog", "Introduce-ti
urmatoarele informatii:", None))
self.label.setText(_translate("Dialog", "Nume ", None))
self.label_2.setText(_translate("Dialog", "Prenume", None))
self.F.setText(_translate("Dialog", "F", None))
self.M.setText(_translate("Dialog", "M", None))
self.label_3.setText(_translate("Dialog", "Sex ",
None))
self.label_4.setText(_translate("Dialog", "Varsta ", None))
self.label_5.setText(_translate("Dialog", "Mail ", None))
self.OK.setText(_translate("Dialog", "OK", None))

`

和我的主要代码:

import sys
from PyQt4.QtGui import *
from PyQt4 import QtCore, QtGui, uic
#from PyQt4.QtWidgets import *
from PyQt4.QtCore import *

from do import Ui_Dialog

class MyApp(QtGui.QMainWindow, Ui_Dialog):
def __init__(self, parent = None):
super(self.__class__, self).__init__()
QtGui.QMainWindow.__init__(self, parent)
central_widget = QtGui.QWidget()
self.setCentralWidget(central_widget)
self.ui = Ui_Dialog()
self.ui.setupUi(central_widget)
self.setupUi(self)
self.OK.clicked.connect(lambda: self.handleButton)
QtGui.QMainWindow.__init__(self)
Ui_Dialog.__init__(self)
self.ui.setupUi(self)


def handleButton(self):
nume = ""
prenume = ""
mail = ""
varsta = ""

nume = self.line_nume.text()
prenume = self.line_prenume.text()
mail = self.line_mail.text()
varsta = self.spin.get()
print(nume, prenume, mail)

def initUi(self):
nume = QtGui.QLabel('Nume')
prenume = QtGui.QLabel('Prenume')
review = QtGui.QLabel('Review')

if __name__ == "__main__":
app = QtGui.QApplication(sys.argv)
window = MyApp()
window.show()
sys.exit(app.exec_())

`

执行时出现一个窗口,左上角有 OK 按钮,按钮下有 lineEdit,如何修复?

最佳答案

如果使用了 5 个模板之一,您必须使用正确的小部件,在您的情况下,我认为您已经使用了 不带按钮的对话框 模板,因此要使用的小部件必须是 QDialog ,而不是 QMainWindow,您还必须调用小部件的构造函数并调用设计的 setupUi() 方法:

import sys
from PyQt4 import QtCore, QtGui

from do import Ui_Dialog

class MyApp(QtGui.QDialog, Ui_Dialog):
def __init__(self, parent = None):
QtGui.QDialog.__init__(self, parent)
self.setupUi(self)
self.OK.clicked.connect(self.handleButton)


def handleButton(self):
nume = ""
prenume = ""
mail = ""
varsta = ""

nume = self.line_nume.text()
prenume = self.line_prenume.text()
mail = self.line_mail.text()
varsta = self.spin.value()
print(nume, prenume, mail, varsta)

if __name__ == "__main__":
app = QtGui.QApplication(sys.argv)
window = MyApp()
window.show()
sys.exit(app.exec_())

enter image description here

关于python - 布局在执行 pyqt 时不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48537856/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com