button.clickd.connect时在函数之间传递变量



我已经用PyQt创建了一个布局,并试图调用一个函数来处理来自QLineEdit的用户输入,但在理解实例时遇到了困难,所有这些都让我头晕目眩,下面是我的代码

def __init__(self):
super().__init__()
self.init_ui()
def init_ui(self):
self.setWindowIcon(QtGui.QIcon("Comfy.png"))
self.setWindowTitle("test")
label = QLabel("Title: ")
wp_edit = QLineEdit()
city = QLabel("City: ")
city_edit = QLineEdit()
phone = QLabel("Phone: ")
phone_edit = QLineEdit()
owner = QLabel("Owner: ")
owner_edit = QLineEdit()
description = QLabel("Description: ")
description_edit = QPlainTextEdit()
logo = QLabel(self)
logo.setAlignment(Qt.AlignCenter)
pixmap = QPixmap("logo.png")
smaller_pixmap = pixmap.scaled(296, 128, Qt.KeepAspectRatio)
logo.setPixmap(smaller_pixmap)
radio1 = QRadioButton("test")
radio2 = QRadioButton("test")
radio3 = QRadioButton("test")
radio4 = QRadioButton("test")
radio5 = QRadioButton("test")
button = QPushButton("Create", self)
button.setIcon(QtGui.QIcon("Check.png"))
button.clicked.connect(self.create_post)
h = QHBoxLayout()
h.addWidget(label)
h.addWidget(wp_edit)
h1 = QHBoxLayout()
h1.addWidget(city)
h1.addWidget(city_edit)
h1.addWidget(phone)
h1.addWidget(phone_edit)
h2 = QHBoxLayout()
h2.addWidget(owner)
h2.addWidget(owner_edit)
h3 = QHBoxLayout()
h3.addWidget(radio1)
h3.addWidget(radio2)
h3.addWidget(radio3)
h3.addWidget(radio4)
h3.addWidget(radio5)
v = QVBoxLayout()
v.addStretch(1)
v.addWidget(logo)
v.addLayout(h)
v.addWidget(description)
v.addWidget(description_edit)
v.addLayout(h1)
v.addLayout(h2)
v.addLayout(h3)
v.addWidget(button)
self.setLayout(v)
self.show()
def create_post(self):
print(self.wp_edit.text())

我尝试了很多我在网上找到的东西,看了一些视频试图理解,但似乎什么都不起作用,我如何在我的create_post函数中使用QLineEdit的用户输入?

您需要在任何地方使用self.wp_edit:

self.wp_edit = QLineEdit()

最新更新