Home > front end >  Python tkinter Variables not defined
Python tkinter Variables not defined

Time:12-09

Advanced baseball metrics calculator. I had it working late one night, forgot to save, now I'm dumbfounded. I keep getting "E1 / E2 / E3 not defined" Any guidance as to what I'm doing wrong will be greatly appreciated.

I'm sure it's something simple

from tkinter import *
import tkinter.messagebox 

    
def pythagentry():
    pythaginput = Tk()
    L0 = Label(pythaginput, text= "Expected Wins", fg="black", font = "Times")
    L0.grid(row=1, column=1)
    L1 = Label(pythaginput, text= "Runs Scored", fg="black", font= "Times")
    L1.grid(row=2, column=1)
    L2 = Label(pythaginput, text= "Runs Allowed", fg="black", font= "Times")
    L2.grid(row=3, column=1)
    L3 = Label(pythaginput, text= "Season Length", fg="black", font= "Times")
    L3.grid(row=4, column=1)
    RS = Entry(pythaginput,bd=5)
    RS.grid (row=2, column=2)
    E2 = Entry(pythaginput,bd=5)
    E2.grid (row=3, column=2)
    E3 = Entry(pythaginput,bd=5)
    E3.grid (row=4, column=2)
    B4 = Button (pythaginput,text='Calculate Pythagorean Wins', command = pythag )
    B4.grid (row=5, column=2)

    
def pythag():
    pythagoutput = Tk()
    L4=Label(pythagoutput,text="Expected Wins:",fg="red")
    L4.grid (row =1, column =1)
    E5=Entry(pythagoutput,bd=5)
    E5.grid(row=4,column=1)
    runsscored= float(E1.get())
    runsallowed= float(E2.get())
    lengthseason= float(E3.get())
    ratio = runsscored / runsallowed
    winpct = pow (ratio, 2) / (pow(ratio, 2)   1)
    expectedwins = lengthseason * winpct
    E5.insert(expectedwins)
    E5['bg']='grey'
    
  

    

top = Tk()
B1=Button(top, text='Calculate Pythagorean Wins',command = pythagentry,bg="blue")
B1.grid(row=1,column=1)

B2=Button(top, text='Calculate wOBA', bg="Blue")
B2.grid(row=1, column=2)

B3=Button(top, text='Calculate RF/9', bg="blue")
B3.grid(row=2, column=1)




top.mainloop()

CodePudding user response:

You can pass the entry widgets as parameters to the pythag function using a lambda in the command callback for the B4 button.

Also Entry.insert takes 2 parameters, the index and the string, so I made adjustments at the bottom of the pythag function as well in the example below.

from tkinter import *
import tkinter.messagebox

def pythagentry():
    pythaginput = Tk()
    L0 = Label(pythaginput, text= "Expected Wins", fg="black", font = "Times")
    L0.grid(row=1, column=1)
    L1 = Label(pythaginput, text= "Runs Scored", fg="black", font= "Times")
    L1.grid(row=2, column=1)
    L2 = Label(pythaginput, text= "Runs Allowed", fg="black", font= "Times")
    L2.grid(row=3, column=1)
    L3 = Label(pythaginput, text= "Season Length", fg="black", font= "Times")
    L3.grid(row=4, column=1)
    RS = Entry(pythaginput,bd=5)
    RS.grid (row=2, column=2)
    E2 = Entry(pythaginput,bd=5)
    E2.grid (row=3, column=2)
    E3 = Entry(pythaginput,bd=5)
    E3.grid (row=4, column=2)
    B4 = Button (pythaginput,text='Calculate Pythagorean Wins', command = lambda: pythag(RS, E2, E3) )
    B4.grid (row=5, column=2)

def pythag(E1, E2, E3):
    pythagoutput = Tk()
    L4=Label(pythagoutput,text="Expected Wins:",fg="red")
    L4.grid (row =1, column =1)
    E5=Entry(pythagoutput, bd=5)
    E5.grid(row=4,column=1)
    runsscored= float(E1.get())
    runsallowed= float(E2.get())
    lengthseason= float(E3.get())
    ratio = runsscored / runsallowed
    winpct = pow (ratio, 2) / (pow(ratio, 2)   1)
    expectedwins = lengthseason * winpct
    E5.insert(0, str(expectedwins))
    E5['bg']='grey'

top = Tk()
B1=Button(top, text='Calculate Pythagorean Wins',command = pythagentry,bg="blue")
B1.grid(row=1,column=1)

B2=Button(top, text='Calculate wOBA', bg="Blue")
B2.grid(row=1, column=2)

B3=Button(top, text='Calculate RF/9', bg="blue")
B3.grid(row=2, column=1)

top.mainloop()
  • Related