跟踪按钮按下哪个框架用户?

时间:2015-05-09 22:47:52

标签: python python-3.x tkinter

我遇到了tkinter的问题,我不确定如何判断用户所在的帧,并将其保存到变量中。我需要(我认为)这些信息能够在多个帧之间切换。

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)

此功能由以下按钮调用:

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_remove和frame2.grid我希望能够有一个变量,如x.grid_remove()y.grid(row =),这样这个函数可以在所有帧之间切换。如果需要,我可以发布剩下的代码,但我试着将其保留在相关的内容中。

谢谢,

完整代码:

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()

1 个答案:

答案 0 :(得分:0)

为什么不在每次帧更改时都尝试设置变量,所以在更改帧的代码中添加另一个参数,在该函数中将获取帧的名称,然后在函数中它将设置a(Global ??)变量到该名称,可以随时读取吗?

所以例如

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

change()是您用来将框架更改为另一个

的功能

这将添加到用于更改框架的函数中,这将使用一个全局变量,可以在任何位置读取,您设置为保存更改时更改的框架的值