2017-11-12 7 views
1

私はPython 3とTkinterを使用して質問とさまざまな回答セットを含む調査フォームを作成しています。Python 3とTkinter:OOPによる質問と変化する回答セットを調査フォームに表示

次のコード行では、アンケートの質問とその回答セットが表示されません。代わりに、彼らは>エラーに0x000000B3CB464400で< メイン .QuestionSetオブジェクトを生成します。

from tkinter import * 

root = Tk() 

question = " " 
def button_press(btn): 
    global question 
    question = btn 
    questionStatement.set(question) 

# Create Class for sets of questions and their possible answers 
class QuestionSet: 

    def __init__(self, questionSet): 

     self.question = questionSet   
     # to display answer options with radio button 
     def answer_options(): 
      for counter in range(len(question)-1): 
       radioButton = RADIOBUTTON(text=question[counter]) 
       radioButton.grid(row=2, column=counter) 
     answer_options() 

# List of questions and their possible answers 
q1 = QuestionSet(["Assessment 1", "Yes", "No"]) 
q2 = QuestionSet(["Assessment 2", "Yes", "No", "Maybe"]) 
q3 = QuestionSet(["Assessment 3", "Agree", "Disagree"]) 
q4 = QuestionSet(["Assessment 4", "Yes", "No"]) 

# Display Questions 
questionStatement = StringVar() 
questionStatement.set(q1) 
questionField = Label(textvariable=questionStatement) 
questionField.grid(columnspan=10, sticky="E") 

# button to select questions to be answered 
button1 = Button(text="1", command=lambda: button_press(q1)) 
button2 = Button(text="2", command=lambda: button_press(q2)) 
button3 = Button(text="3", command=lambda: button_press(q3)) 
button4 = Button(text="4", command=lambda: button_press(q4)) 

button1.grid(row=2, column=0) 
button2.grid(row=2, column=1) 
button3.grid(row=2, column=2) 
button4.grid(row=2, column=3) 

root.mainloop() 
+0

をご 'あなたはguestion''を参照してください、私はあなたがself.question' 'を指すことを意味だと思いrange'。 'questionStatement.set(q1)'を呼び出すと、文字列が必要なときに 'q1'のインスタンス参照に設定します。 'RadioButton'は大文字ではなくカメルケースのタイトルです。また、最初の引数として' root'を期待しています。 – SolarFactories

答えて

0

クラスの文字列表現をカスタマイズするには、自分のクラスの__str__メソッドをオーバーライドすることができます。文字列を返す必要があります。あなたのケースでは、あなたが行うことができます:インサイド

class QuestionSet: 
    def __init__(self, *args, **kwargs): 
     self.question = questionSet 

    def __str__(self): 
     return self.question[0] 
関連する問題