【发布时间】:2021-10-02 17:51:37
【问题描述】:
我试图在从组合框中选择文本框选项并在选择图像选项后添加图像后创建一个文本框。我需要这方面的帮助。
我知道 self.line = QLineEdit 将创建一个文本框,对于一个组合框,我可以使用 combo.activated[str].connect(self.onChanged) 来检测组合框的变化并调用该函数,但我不知道为什么我把它放在 onChanged 函数后它不起作用。
import sys
from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
class Example(QMainWindow):
def __init__(self):
super().__init__()
combo = QComboBox(self)
combo.addItem("textbox")
combo.addItem("image")
combo.move(50, 50)
self.qlabel = QLabel(self)
self.qlabel.move(50,16)
combo.activated[str].connect(self.onChanged)
self.setGeometry(50,50,320,200)
self.setWindowTitle("Programme")
self.show()
def onChanged(self, text):
if text == 'textbox':
self.line = QLineEdit(self)
if text == 'image':
self.im = QPixmap("image.jpg")
self.label = QLabel()
self.label.setPixmap(self.im)
if __name__ == '__main__':
app = QApplication(sys.argv)
ex = Example()
sys.exit(app.exec_())
【问题讨论】: