【问题标题】:Python 2.7, Tkinter multiple choice quiz (notebook layout, and radio button help)Python 2.7、Tkinter 多项选择测验(笔记本布局和单选按钮帮助)
【发布时间】:2018-02-09 04:22:27
【问题描述】:

我是一名学生,作为初学者,我对 Python 了解不多。但是,我决定做一个多项选择测验,其中没有答案是不正确的,但所有答案加起来就是总分。我已将每个问题页面布置到其自己的类中,例如类 Question1(tk.Frame):。我使用的一些代码来自互联网的帮助......

我的第一个问题是,如何让每个类成为自己的框架(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()

【问题讨论】:

    标签: python python-2.7 tkinter radio-button tk


    【解决方案1】:

    我的第一个问题是,如何让每个类成为自己的框架(frame_1、frame_2 等...),以便我可以在 Notebook 布局中使用它?

    您无需做太多事情就可以将每个课程变成笔记本选项卡。您已经完成了大部分工作。您的类(HomeQuestion1 等)已经是框架,因此您可以直接使用它们。它们每个都将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())
    

    【讨论】:

    • 如何制作控制器?我已经迷路了xD
    • @GauravKararia:我不确定。这是你的代码,不是我的。也许回到你复制代码的地方,看看它做了什么来创建一个控制器。
    猜你喜欢
    • 2020-12-23
    • 1970-01-01
    • 1970-01-01
    • 2018-04-05
    • 2017-09-07
    • 1970-01-01
    • 2019-10-07
    • 1970-01-01
    • 2013-02-24
    相关资源
    最近更新 更多