2
votes

Just messing around with tkinter for the first time and I dont know why my buttons are not working. Basically the input_grade is supposed to get a value from entry_for_grades which is quiz_amount and just print it on the same window but I keep getting back 0

I looked up and thought I was suppose to use lambda but it keeps giving me 0 regardless of what I have in entry and press ok

from tkinter import *
quiz_amount = IntVar()

def quiz():
    quiz_window = Tk()
    window(quiz_window)
    quiz_window.title('Quiz Grades')
    width = quiz_window.winfo_width()
    height= quiz_window.winfo_height()
    quiz_window.geometry('{}x{}'.format(width, height))
    entry_for_grades = Entry(quiz_window, textvariable=quiz_amount)
    entry_for_grades.pack()
    grade_amount = Button(quiz_window,text='Ok',command=lambda:see_text(quiz_window)).place(x=85,y=60)   
def see_text(window):
    Label(window,text=float(quiz_amount.get())).place(x=85,y=90)
1
You can't create Tkinter Vars until after you've called Tk() - I'm surprised this isn't causing an error, I guess something's changed since the version I use. - jasonharper
You are using this to make several windows, aren't you? The problem here is the IntVar is associated with the first window you created and can't be used in others. You need to use Toplevel instead of Tk to make additional windows, then you can use the IntVar in any of them. - Novel

1 Answers

1
votes

I used your code and made it a Minimal, Reproducible Example (I think) and it works fine for me.

Here is the code:

from tkinter import *


def quiz(quiz_window):
    # window(quiz_window)
    quiz_window.title('Quiz Grades')
    width = quiz_window.winfo_reqwidth()
    height= quiz_window.winfo_reqheight()
    quiz_window.geometry('{}x{}'.format(width, height))
    entry_for_grades = Entry(quiz_window, textvariable=quiz_amount)
    entry_for_grades.pack()
    grade_amount = Button(quiz_window,text='Ok',command=lambda:see_text(quiz_window)).place(x=85,y=60)
def see_text(window):
    Label(window,text=float(quiz_amount.get())).place(x=85,y=90)


if __name__ == '__main__':
    quiz_window = Tk()
    quiz_amount = IntVar()
    quiz(quiz_window)
    quiz_window.mainloop()

The only problem I can think of is, like Novel said, you are using more than one Tk() window and trying to pass variables between these windows.

You should almost never use more than one Tk() window since you cannot pass information between two Tk() windows. Instead use TopLevel() windows, information can flow freely between Tk() and TopLevel() windows.