2017-04-05 602 views
0

我是初學者,使用GUI和PYQT。我想要做的是動態建立一個QComboBox和QLineEdit的網格。從QComboBox中,您可以選擇一個選項,並從該選項中選擇一些數字填寫相應的QLineEdit。我遇到的問題是創建第一個QComboBox和第一個QLineEdit框之間的鏈接。我可以爲每一行創建一個函數,但我想知道更好的方法。我會發布一些示例代碼。感謝您提供任何幫助或建議。PYQT訪問和更改動態添加的控件

import sys 
from PyQt5.QtCore import QCoreApplication 
from PyQt5.QtGui import * 
from PyQt5.QtWidgets import * 

class window(QMainWindow): 

    def __init__(self): 
     super(window, self).__init__() 
     self.setGeometry(50, 50, 700, 600) 

     self.home() 

    def home(self): 
     Test1Choices = ['Test1:','Choice1', 'Choice2', 'Choice3', 'Choice4','Choice5', 'Choice6', 'Choice7', 'Choice8', 'Choice9'] 
     Test2Choices= ['Test2:','1','2','3','4','5','6','7','8','9','10','11','12','13','14','15'] 
     for i in range(0,10): 
      Choice1ComboBox = QComboBox(self) 
      Choice1ComboBox.addItems(Test1Choices) 
      Choice1ComboBox.resize(150,25) 
      Choice1ComboBox.move(30,(150+(i*35))) 
      Choice1ComboBox.setCurrentIndex(2) 

      Choice2ComboBox = QComboBox(self) 
      Choice2ComboBox.setObjectName("Choice2ComboBox"+str(i)) 
      Choice2ComboBox.addItems(Test2Choices) 
      Choice2ComboBox.resize(75,25) 
      Choice2ComboBox.move(200,(150+(i*35))) 
      Choice2ComboBox.setCurrentIndex(2) 
      Choice2ComboBox.activated[str].connect(self.doSomething) 

      numTextBox = QLineEdit(self) 
      numTextBox.setObjectName("numBox"+str(i)) 
      numTextBox.move(325,(150+(i*35))) 
      numTextBox.resize(35,25) 

      result1TextBox = QLineEdit(self) 
      result1TextBox.setObjectName("result1Box"+str(i)) 
      result1TextBox.move(400,(150+(i*35))) 
      result1TextBox.resize(100,25) 
      result1TextBox.setEnabled(0) 

      result2TextBox = QLineEdit(self) 
      result2TextBox.setObjectName("result2Box"+str(i)) 
      result2TextBox.move(525,(150+(i*35))) 
      result2TextBox.resize(100,25) 
      result2TextBox.setEnabled(0) 

     self.show() 

    def doSomething(self): 
     numbers=['result1','result2','result3','result4','result5','result6','result7','result8','result9','result10','result11','result12','result13','result14','result15'] 

def run(): 
    app = QApplication(sys.argv) 
    Gui = window() 
    sys.exit(app.exec_()) 


run() 

總結我想在選擇QComboBox的指數帶來。然後使用該索引號來引用「數字」數組中的答案。那麼這導致QLineEdit是同一行

+0

我看到你有組合框的2列,QLineEdit的一列,另外2列的QLineEdit被禁用,它們之間有什麼關係。你的描述沒有關於他們。 – eyllanesc

+0

這是計算一組電線的總面積。第一個組合框用於每根導線的絕緣類型,而第二個組合框是導線尺寸。所以如果你有絕緣類型的THWN,並且尺寸是12awg,那麼0.0133應該出現在同一行的第一個禁用的QLineEdit中。我試圖改變代碼以使其更通用。 – laxer

+0

其他QLineEdits已禁用不會使用它們嗎?你只想使用2 QComboBox和QLineEdit。 – eyllanesc

回答

0

我們使用sender()來獲取發出信號的對象在打印,接下來我們來看看與setObjectName()該對象的名稱,我們搜索索引,然後我們得到其他對象findChildren(),例如輸出將是所選文本的聯合。

添加名稱Choice1ComboBox:

Choice1ComboBox.setObjectName("Choice1ComboBox"+str(i)) 

DoSomething的功能:

def doSomething(self, _): 
    sender = self.sender() 

    l = sender.objectName().split("Choice1ComboBox") 
    if len(l) > 1: 
     number = l[1] 
    else: 
     number = sender.objectName().split("Choice2ComboBox")[1] 

    combo1 = self.findChildren(QComboBox, "Choice1ComboBox"+number)[0] 
    combo2 = self.findChildren(QComboBox, "Choice2ComboBox"+number)[0] 
    obj = self.findChildren(QLineEdit, "numBox"+number)[0] 
    obj.setText(combo1.currentText() + " " + combo2.currentText()) 

完整代碼:

import sys 
from PyQt5.QtCore import QCoreApplication 
from PyQt5.QtGui import * 
from PyQt5.QtWidgets import * 

class window(QMainWindow): 

    def __init__(self): 
     super(window, self).__init__() 
     self.setGeometry(50, 50, 700, 600) 

     self.home() 

    def home(self): 
     Test1Choices = ['Test1:','Choice1', 'Choice2', 'Choice3', 'Choice4','Choice5', 'Choice6', 'Choice7', 'Choice8', 'Choice9'] 
     Test2Choices= ['Test2:','1','2','3','4','5','6','7','8','9','10','11','12','13','14','15'] 
     for i in range(0,10): 
      Choice1ComboBox = QComboBox(self) 
      Choice1ComboBox.setObjectName("Choice1ComboBox"+str(i)) 
      Choice1ComboBox.addItems(Test1Choices) 
      Choice1ComboBox.resize(150,25) 
      Choice1ComboBox.move(30,(150+(i*35))) 
      Choice1ComboBox.setCurrentIndex(2) 
      Choice1ComboBox.activated[str].connect(self.doSomething) 

      Choice2ComboBox = QComboBox(self) 
      Choice2ComboBox.setObjectName("Choice2ComboBox"+str(i)) 
      Choice2ComboBox.addItems(Test2Choices) 
      Choice2ComboBox.resize(75,25) 
      Choice2ComboBox.move(200,(150+(i*35))) 
      Choice2ComboBox.setCurrentIndex(2) 
      Choice2ComboBox.activated[str].connect(self.doSomething) 

      numTextBox = QLineEdit(self) 
      numTextBox.setObjectName("numBox"+str(i)) 
      numTextBox.move(325,(150+(i*35))) 
      numTextBox.resize(35,25) 

      result1TextBox = QLineEdit(self) 
      result1TextBox.setObjectName("result1Box"+str(i)) 
      result1TextBox.move(400,(150+(i*35))) 
      result1TextBox.resize(100,25) 
      result1TextBox.setEnabled(0) 

      result2TextBox = QLineEdit(self) 
      result2TextBox.setObjectName("result2Box"+str(i)) 
      result2TextBox.move(525,(150+(i*35))) 
      result2TextBox.resize(100,25) 
      result2TextBox.setEnabled(0) 

     self.show() 

    def doSomething(self, _): 
     sender = self.sender() 

     l = sender.objectName().split("Choice1ComboBox") 
     if len(l) > 1: 
      number = l[1] 
     else: 
      number = sender.objectName().split("Choice2ComboBox")[1] 

     combo1 = self.findChildren(QComboBox, "Choice1ComboBox"+number)[0] 
     combo2 = self.findChildren(QComboBox, "Choice2ComboBox"+number)[0] 
     obj = self.findChildren(QLineEdit, "numBox"+number)[0] 
     obj.setText(combo1.currentText() + " " + combo2.currentText()) 




def run(): 
    app = QApplication(sys.argv) 
    Gui = window() 
    sys.exit(app.exec_()) 


run() 
+0

就是這樣!非常感謝!!我花了更多的時間比我想承認試圖弄清楚。我感謝你的幫助! – laxer