2017-08-31 46 views
2

我是一名學生,作爲初學者,我不太瞭解Python。然而,我決定做一個多選題測驗,其中沒有答案是不正確的,但所有的答案加起來給你一個總分。我已經將每個問題頁面放到自己的類中,如課程Question1(tk.Frame):.我使用的一些代碼是從互聯網的幫助...Python 2.7,Tkinter多選題測驗(筆記本佈局和單選按鈕幫助)

我的第一個問題是,我如何讓每個類成爲自己的幀(frame_1,frame_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,Question1等)已經是框架,所以你可以直接使用它們。他們每個人都以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") 
... 

我的第二個問題是,我如何才能從單選按鈕值(值= 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:我不確定。這是你的代碼不是我的。也許回到你複製代碼的地方,看看它是如何創建一個控制器的。 –