【问题标题】:how to create a text box after a selecting a dropdown in combobox [duplicate]在组合框中选择下拉列表后如何创建文本框[重复]
【发布时间】: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_())

【问题讨论】:

    标签: python pyqt pyqt5


    【解决方案1】:

    使用已显示的父项创建的项目不会自动显示,并且必须显式调用show()setVisible(True)除非 它们被添加到布局中。

    在您的情况下,您需要添加self.line.show()self.label.show(),但您还需要使用父参数创建标签:self.label = QLabel(self),否则它将被视为顶级小部件,它将被显示在单独的窗口中。

    请考虑创建这样的小部件可能会产生一些问题,尤其是在多次选择时:如果您希望新选择覆盖之前的小部件,则需要将其删除,并且在任何情况下,您都应该将小部件添加到布局中(无论如何都应该创建它,因为通常不鼓励使用固定的几何图形)。如上所述,向布局添加新小部件会自动显示它,因此无需再显式显示它。

    import sys
    from PyQt5.QtWidgets import *
    from PyQt5.QtGui import * 
    
    class Example(QMainWindow):
        def __init__(self):
            super().__init__()
            self.setWindowTitle("Programme")
    
            central = QWidget()
            self.setCentralWidget(central)
            self.layout = QVBoxLayout(central)
    
            combo = QComboBox(self)
            combo.addItems(("textbox", "image"))
            self.layout.addWidget(combo)
    
            self.overwriteCheck = QCheckBox('Overwrite')
            self.layout.addWidget(self.overwriteCheck)
            self.lastWidget = None
    
            combo.activated[str].connect(self.onChanged)      
            self.show()
    
        def onChanged(self, text):
            if self.lastWidget and self.overwriteCheck.isChecked():
                self.layout.removeWidget(self.lastWidget)
                self.lastWidget.deleteLater()
    
            if text == 'textbox':
                self.lastWidget = QLineEdit()
            elif text == 'image':
                self.lastWidget = QLabel()
                self.lastWidget.setPixmap(QPixmap("image.jpg"))
    
            self.layout.addWidget(self.lastWidget)
    
            if self.overwriteCheck.isChecked():
                QApplication.processEvents()
                self.adjustSize()
    
    if __name__ == '__main__':
        app = QApplication(sys.argv)
        ex = Example()
        sys.exit(app.exec_())
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-02-20
      • 1970-01-01
      • 1970-01-01
      • 2016-05-05
      • 2013-06-21
      • 2017-10-17
      • 1970-01-01
      相关资源
      最近更新 更多