2017-02-09 180 views
0

我正在寫一個QtWidget,使用PyQt5顯示QComboBox(下拉菜單)。然而,它是空的。有誰知道爲什麼?QtWidget顯示QComboBox出現空

import sys 
from PyQt5 import QtCore, QtGui, QtWidgets 

class Test(QtWidgets.QWidget): 
    def __init__(self, formname = "Test"): 
     super().__init__() 
     self.formname = formname 
     self.impute_methods = ["Method 1", "Method 2"] 

    def setupUi(self, Form): 
     Form.setObjectName(self.formname) 
     Form.resize(1154, 902) 
     self._create_buttons(Form) 
     self.retranslateUi(Form) 
     QtCore.QMetaObject.connectSlotsByName(Form) 

    def _create_buttons(self, Form): 
     self.test_box = QtWidgets.QComboBox() 
     self.test_box.addItems(self.impute_methods) 
     self.test_box.setGeometry(QtCore.QRect(110, 190, 150, 27)) 
     self.test_box.setObjectName("test_box") 

    def selectionchange(self, i): 
     print(i) 

    def retranslateUi(self, Form): 
     _translate = QtCore.QCoreApplication.translate 
     Form.setWindowTitle(_translate(self.formname, "Test")) 

if __name__ == "__main__": 
    app = QtWidgets.QApplication(sys.argv) 
    tester = QtWidgets.QDialog() 
    ui = Test() 
    ui.setupUi(tester) 
    tester.show() 
    sys.exit(app.exec_()) 

這裏是我所看到的截圖:

enter image description here

+0

因爲您沒有使用佈局。 – ekhumoro

+0

你可以擴展一下,也許在評論中?謝謝。 – Alex

+0

'layout = QtWidgets.QVBoxLayout(self); layout.addWidget(self.test_box)'。 [佈局管理](https://doc.qt.io/qt-4.8/layout.html)。 – ekhumoro

回答

0

集父母的組合框放置在Test()

def _create_buttons(self, Form): 
    self.test_box = QtWidgets.QComboBox(self) 
    ... 

和用戶界面,將它放在對話框:

... 
ui = Test() 
ui.setParent(tester) 
...