2017-08-31 26 views
2

私は学生です。初心者としては、私は多くのPythonを知りません。しかし、私は、答えが間違っていない複数の選択肢のクイズを作ることにしましたが、すべての答えが合計して合計得点になります。私は各質問ページをクラスQuestion1(tk.Frame)のように独自のクラスにレイアウトしました:私が使ったコードの一部は、インターネットの助けを借りて作られたものです。Python 2.7、Tkinterの多肢選択式クイズ(ノートブックレイアウトとラジオボタンのヘルプ)

私の最初の質問は、どのようにして各クラスを独自のフレーム(frame_1、frame_2など)にするのですか?それはノートのレイアウトですか?

私の2番目の質問は、ラジオボタン(値= 1など)から値を取得して、保存可能な.csvファイルに送信する方法です。私の心の中には何もありません...

ヘルプは高く評価されるでしょう!

from Tkinter import * 
import ttk 
import Tkinter as tk 

LARGE_FONT= ("Arial", 12) 

def sel(): 
      selection = "You selected " + str(var.get()) 
      label.config(text = selection) 

#def answerout(): 

root = tk.Tk() 
var = tk.IntVar() 
notebook = ttk.Notebook(root) 
frame_1 = ttk.Frame(notebook) 
frame_2 = ttk.Frame(notebook) 
frame_3 = ttk.Frame(notebook) 
frame_4 = ttk.Frame(notebook) 
notebook.add(frame_1, text='Home') 
notebook.add(frame_2, text='Question 1') 
notebook.add(frame_3, text='Question 2') 
notebook.add(frame_4, text='Question 3') 
notebook.pack(expand=1, fill="both") 

class Home(tk.Frame): 
    frame1 = Frame(frame_1) 
    frame1.pack(side=TOP) 
    def __init__(root, parent, controller): 
     frame1 = Frame(frame_1) 
     frame1.pack(side=TOP) 
     tk.Frame.__init__(root,parent) 
     label = tk.Label(root, text="This program tries to understand characteristic features of individual students.", font=LARGE_FONT) 
     label1 = tk.Label(root, text="Please click start to begin!", font=LARGE_FONT) 
     label.pack(pady=10,padx=10) 
     label1.pack(pady=10,padx=10) 
     button = tk.Button(root, text="Start!", 
          command=lambda: controller.show_frame(Question1)) 
     button.pack() 
     button2 = tk.Button(root, text="Exit", 
          command=lambda: app.destroy()) 
     button2.pack() 

class Question1(tk.Frame): 
    def __init__(root, parent, controller): 
     tk.Frame.__init__(root, parent) 
     label1 = tk.Label(root, text="I make choices based on what I think, not on what I feel.", font=LARGE_FONT) 
     label1.pack(pady=10,padx=10) 
     R1 = Radiobutton(root, text="Not Often", variable=var, value=1, 
        command=sel) 
     R1.pack(anchor = W) 

     R2 = Radiobutton(root, text="Sometimes", variable=var, value=2, 
        command=sel) 
     R2.pack(anchor = W) 

     R3 = Radiobutton(root, text="Often", variable=var, value=3, 
        command=sel) 
     R3.pack(anchor = W) 
     button1 = tk.Button(root, text="Back to Home", 
          command=lambda: controller.show_frame(Home)) 
     button1.pack() 
     button2 = tk.Button(root, text="Next", 
          command=lambda: controller.show_frame(Question2)) 
     button2.pack() 

class Question2(tk.Frame): 
    def __init__(root, parent, controller): 
     tk.Frame.__init__(root, parent) 
     label = tk.Label(root, text="I challenge people if I don't think they are right.", font=LARGE_FONT) 
     label.pack(pady=10,padx=10) 
     R4 = Radiobutton(root, text="Not Often", variable=var, value=1, 
        command=sel) 
     R4.pack(anchor = W) 

     R5 = Radiobutton(root, text="Sometimes", variable=var, value=2, 
        command=sel) 
     R5.pack(anchor = W) 

     R6 = Radiobutton(root, text="Often", variable=var, value=3, 
        command=sel) 
     R6.pack(anchor = W) 
     button2 = tk.Button(root, text="Back", 
          command=lambda: controller.show_frame(Question1)) 
     button2.pack() 
     button3 = tk.Button(root, text="Next", 
          command=lambda: controller.show_frame(Question3)) 
     button3.pack() 
     button1 = tk.Button(root, text="Back to Home", 
          command=lambda: controller.show_frame(Home)) 
     button1.pack() 

class Question3(tk.Frame): 
    def __init__(root, parent, controller): 
     tk.Frame.__init__(root, parent) 
     label = tk.Label(root, text="I can change and fit into new situations easily.", font=LARGE_FONT) 
     label.pack(pady=10,padx=10) 
     R7 = Radiobutton(root, text="Not Often", variable=var, value=1, 
        command=sel) 
     R7.pack(anchor = W) 

     R8 = Radiobutton(root, text="Sometimes", variable=var, value=2, 
        command=sel) 
     R8.pack(anchor = W) 

     R9 = Radiobutton(root, text="Often", variable=var, value=3, 
        command=sel) 
     R9.pack(anchor = W) 
     button2 = tk.Button(root, text="Back", 
          command=lambda: controller.show_frame(Question2)) 
     button2.pack() 
     button3 = tk.Button(root, text="Next (GOES HP)", 
          command=lambda: controller.show_frame(Home)) 
     button3.pack() 
     button1 = tk.Button(root, text="Back to Home", 
          command=lambda: controller.show_frame(Home)) 

label = Label(root) 
label.pack() 
app = root 
app.geometry('1024x512') 
app.wm_title('Brain Thinking Model') 
app.iconbitmap(r'fav.ico') 
app.mainloop() 
root.mainloop() 

答えて

0

私の最初の質問は、私は各クラスが独自のフレーム(FRAME_1、frame_2、等...)になってもらうんので、私はノートブックのレイアウトでそれを使用する方法、ありますか?

各クラスをノートブックタブにする必要はあまりありません。あなたはすでに仕事のほとんどをやっています。あなたのクラス(Home、など)はすでにフレームであるため、それらを直接使用することができます。彼らはそれぞれ引数としてcontrollerを受け取るので、クラスのインスタンスよりも作成してからインスタンスをノートブックに追加する必要があります。

controller = <something you did not include in your question> 

notebook = ttk.Notebook(root) 
frame_1 = Home(notebook, controller) 
frame_2 = Question1(notebook, controller) 
... 
notebook.add(frame_1, text="Home") 
notebook.add(frame_2, text="Question 1") 
... 

私の2番目の質問は、私はラジオボタン(値= 1、など)からの値を格納することが可能.csvファイルに送信するのですかどのように、ありますか?

ラジオボタンに関連付けられた変数への参照が必要です。これで、getメソッドを呼び出して値を取得できます。

var = tk.IntVar() 
... 
R1 = Radiobutton(..., variable=var, value=1, ...) 
... 
value = var.get() 

クラスを使用しているので、varをインスタンス変数にすることをお勧めします。つまり、グローバル変数を作成する代わりに、ラジオボタンを作成する関数で作成します。これをインスタンス変数に割り当てます。インスタンス変数は、クラスのインスタンスへの参照を持つプログラム内の任意の場所で参照できます。

class Home(...): 
    def __init__(...): 
     self.var = tk.IntVar() 
     R1 = Radiobutton(..., variable=var, value=1, ...) 
     ... 

frame_1 = Home(...) 
... 
print("the value is:", frame_1.var.get()) 
+0

コントローラを作成するにはどうすればよいですか?私はほとんどが既に失われているxD –

+0

@ GauravKararia:わからない。これは私のコードではありません。コードをコピーした場所に戻って、コントローラを作成するために何が行われたのかを確認してください。 –

関連する問題