track which frame user is in on button press?

i've run into a problem with tkinter, where i'm unsure of how to tell what frame the user is in, and save that to a variable. i need (i think) this information to be able to switch between multiple frames.

def check(value):  # checks to see if the button that was click was the correct answer

if value:
    print("you picked the right answer!")
    answers.increase()
    frame1.grid_remove()
    frame2.grid(row=5)

else:
    print("sorry thats not right")
    frame1.grid_remove()
    frame2.grid(row=5)

this function is called by a button like:

q1choice1 = Button(frame1, height=2, width=50, text=question1[1].text, command=lambda: check(question1[1].value))
q1choice2 = Button(frame1, height=2, width=50, text=question1[2].text, command=lambda: check(question1[2].value))
q1choice3 = Button(frame1, height=2, width=50, text=question1[3].text, command=lambda: check(question1[3].value))
q1choice4 = Button(frame1, height=2, width=50, text=question1[4].text, command=lambda: check(question1[4].value))

instead of frame1.grid_remove and frame2.grid i would like to be able to have a variable such as x.grid_remove() y.grid(row=) so that this on function can switch between all frames. i can post the rest of the code if it's needed but i tried to just keep it to what is relevant.

thanks,

full code:

from tkinter import *

# class for the correct answers in choices


class Correct(object):
    value = True

    def __init__(self, text):
        self.text = text

# class for incorrect answers


class Incorrect(object):
    value = False

    def __init__(self, text):
        self.text = text

# this class allows me to increment a var for num of correct answers


class CountRight(object):
    def __init__(self):
        self.right = 0

    def increase(self):
        self.right +=1


answers = CountRight()
framecount = CountRight()
framecount.increase()

def check(value):  # checks to see if the button that was click was the correct answer
    framecount.increase()
    framestring = ["frame" , framecount.right]
    framestring = str(framestring)
    framestring = "".join(framestring)
    framestring = framestring.replace("'", "")
    framestring = framestring.replace(",", "")
    framestring = framestring.replace(" ", "")


    print(framestring)
    if value:
        print("you picked the right answer!")
        answers.increase()
        framestring.grid_remove()
        framestring.grid(row=5)

    else:
        print("sorry thats not right")
        frame1.grid_remove()
        frame2.grid_remove()


def center(win):

    win.update_idletasks()
    width = win.winfo_width()
    frm_width = win.winfo_rootx() - win.winfo_x()
    win_width = width + 2 * frm_width
    height = win.winfo_height()
    titlebar_height = win.winfo_rooty() - win.winfo_y()
    win_height = height + titlebar_height + frm_width
    x = win.winfo_screenwidth() // 2 - win_width // 2
    y = win.winfo_screenheight() // 2 - win_height // 2
    win.geometry('{}x{}+{}+{}'.format(width, height, x, y))
    win.deiconify()


question1 = ["question", Correct("A:"), Incorrect("B:"),
             Incorrect("C:"), Incorrect("D:")]

master = Tk()
toplabel = Label(master,font="big", text="quiz!")
toplabel.grid(row=0)
master.geometry("350x350")
center(master)

#  ********************  FRAME ONE *********************

frame1 = Frame(master)
qlabel1 = Label(frame1, text=question1[0])
q1choice1 = Button(frame1, height=2, width=50, text=question1[1].text, command=lambda: check(question1[1].value))
q1choice2 = Button(frame1, height=2, width=50, text=question1[2].text, command=lambda: check(question1[2].value))
q1choice3 = Button(frame1, height=2, width=50, text=question1[3].text, command=lambda: check(question1[3].value))
q1choice4 = Button(frame1, height=2, width=50, text=question1[4].text, command=lambda: check(question1[4].value))




frame1.grid(row=5, sticky="nsew")
qlabel1.grid(row=0, column=0, rowspan=5, columnspan=5, sticky="nsew")
q1choice1.grid(row=6, column=0, rowspan=5, columnspan=5, sticky="nsew")
q1choice2.grid(row=11, column=0, rowspan=5, columnspan=5, sticky="nsew")
q1choice3.grid(row=16, column=0, rowspan=5, columnspan=5, sticky="nsew")
q1choice4.grid(row=21, column=0, rowspan=5, columnspan=5, sticky="nsew")

#  ********************  FRAME TWO *********************

frame2 = Frame(master)

question2 = ["question", Incorrect("A:"), Incorrect("B:"),
             Correct("C:"), Incorrect("D:")]
qlabel2 = Label(frame2, text=question2[0])


q2choice1 = Button(frame2, height=2, width=50, text=question2[1].text, command=lambda: check(question2[1].value))
q2choice2 = Button(frame2, height=2, width=50, text=question2[2].text, command=lambda: check(question2[2].value))
q2choice3 = Button(frame2, height=2, width=50, text=question2[3].text, command=lambda: check(question2[3].value))
q2choice4 = Button(frame2, height=2, width=50, text=question2[4].text, command=lambda: check(question2[4].value))


qlabel2.grid(row=0, column=0, rowspan=5, columnspan=5, sticky="n")
q2choice1.grid(row=6, column=0, rowspan=5, columnspan=5, sticky="n")
q2choice2.grid(row=11, column=0, rowspan=5, columnspan=5, sticky="n")
q2choice3.grid(row=16, column=0, rowspan=5, columnspan=5, sticky="n")
q2choice4.grid(row=21, column=0, rowspan=5, columnspan=5, sticky="n")



mainloop()

Answers


Why don't you try to set the variable every time the frame changes, so in the code that changes the frame add another parameter, in the function which will take the name of the frame and then in the function it will set a (Global??) variable to that name which can be read at any time?

so for example

def change(..., newFrame):
    global curFrame
    curFrame = newFrame

change() being the function in which you use to change the frame to another

This will be added to the function you use to change the frame, and this will make use of a global variable which can be read any where, which you set to hold the value of the frame you change to when you change it


Need Your Help

C# .net Datagrid binding from different sources

c# .net datagrid

I have a problem using Datagrid in C# .net.

Does google scripts have a Trimmean method?

javascript google-app-engine google-apps-script google-apps

I am creating a Google script in Google Drive that loads the same data type from multiple youtube videos (uploaded by same channel) and need to create a trimmed mean of from that array of data. I am

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.