为什么我不能输出PYQT的整数?

时间:2015-12-12 14:05:51

标签: python pyqt

def Q2_Level_1(self, crop_type):
    #this is the layout that is switched to when Level 1 is clicked

    self.question_2_label = QLabel("Q2) What is the definition of Price Elasticity of Demand?", self) #label for title
    self.question_2_label.setStyleSheet('font: 25pt; font-family: Arial Black; color: white; background-color: lightblue; ')

    self.question_2_radio_buttons = RadioButtonWidget("", "",("Responsiveness of the quantity demanded of a good or service relative to a change in its price, ceteris paribus", "BLOB", "blah blah blah", "blah blah blah")) #random answers for radio button
    self.question_2_radio_buttons.setStyleSheet('font-size: 13pt; font family: Arial; color: black; blackground-color: lightgreen; ') #changing the format of the label

    self.next_question_button_2 = QPushButton("Next Question") #button for changing question
    self.next_question_button_2.setStyleSheet('font-size: 10pt; font-family: Arial Black; color: white; background-color: red; ') #changing the format of the button
    self.next_question_button_2.clicked.connect(self.new_layout_5) #this connecion connects to the new layout (question) when clicked

    self.quit_button_2 = QPushButton("Quit", self) #button for exiting the application
    self.quit_button_2.setStyleSheet('font-size: 10pt; font-family: Arial Black; color: white; background-color: red; ') #changing the format of the button
    self.quit_button_2.clicked.connect(QCoreApplication.instance().quit) #this connection exits the application

    if crop_type == 1: #operation that is called depedning on what button is clicked THIS IS WHERE THE PROBLEM LIES
        self.score_Q1_L1 = int(10) #creating a variable of 10
        self.total_score_Q1_L1 = QLabel("Score: ", str(self.score_Q1_L1))#attempting to add that variable to the score

    else:
        #self.score_Q1_L1 = 0
        #self.total_score_Q1_L1 = QLabel("No score: ")
        pass

    #setting the layout commands
    self.layout_4 = QVBoxLayout()
    self.layout_4.addWidget(self.question_2_label)
    self.layout_4.addWidget(self.total_score_Q1_L1)
    self.layout_4.addWidget(self.question_2_radio_buttons)
    self.layout_4.addWidget(self.next_question_button_2)
    self.layout_4.addWidget(self.quit_button_2)

    self.level_1_widget_1 = QWidget()
    self.level_1_widget_1.setLayout(self.layout_4)

def new_layout_4(self):
    crop_type = self.question_1_radio_buttons.selected_button() #get the radio that was selected

    self.Q2_Level_1(crop_type) #call upon the layout
    self.stacked_layout.addWidget(self.level_1_widget_1) #add this to the stack
    self.stacked_layout.setCurrentIndex(4) #change the visible layout in the stack

因此,当我执行代码时,会出现以下错误:

self.total_score_Q1_L1 = QLabel("Score: ", str(self.score_Q1_L1))#attempting to add that variable to the score
TypeError: arguments did not match any overloaded call:
  QLabel(QWidget parent=None, Qt.WindowFlags flags=0): argument 1 has unexpected type 'str'
  QLabel(str, QWidget parent=None, Qt.WindowFlags flags=0): argument 2 has unexpected type 'str'

1 个答案:

答案 0 :(得分:0)

QLabel需要1个字符串作为标签的文本,但是您发送2个字符串。 将self.score_Q1_L1添加到第一个字符串:

self.total_score_Q1_L1 = QLabel("Score: {}".format(self.score_Q1_L1))