I have created some entry widgets for user to input points for different teams. I would like to know, how is it possible assign them to particular list and sum those points to print the result...
My code below showed what have I tried, but it doesn't work and I receive an error:
ValueError: invalid literal for int() with base 10: ''
Also, if I will remove sum functions, numbers does not appear in lists. Can anyone explain me how to solve this problem? My code:
from tkinter import *
import tkinter.ttk as ttk
class CollegeApp(Tk):
def __init__(self):
Tk.__init__(self)
container = ttk.Frame(self)
container.pack(side="top", fill="both", expand=True)
self.frames = {}
for F in (StartPage, counterPage):
frame = F(container, self)
self.frames[F] = frame
frame.grid(row=0, column=0, sticky="nsew")
self.show_frame(StartPage)
self.lift()
def show_frame(self, cont):
frame = self.frames[cont]
frame.tkraise()
class StartPage(ttk.Frame):
def __init__(self, parent, controller):
self.controller = controller
ttk.Frame.__init__(self, parent)
self.startMenu()
def startMenu(self):
heading = Label(self, text="College Tournament Points\n Count Software",
font=('Arial', 25))
heading.grid(row=0, column=0, columnspan=2, padx=240, pady=40)
start_Btn = Button(self, text="START", font="Arial 16", width=8, height=2,
command=lambda: self.controller.show_frame(counterPage))
start_Btn.grid(row=1, column=0, columnspan=2, padx=30, pady=5)
exit_Btn = Button(self, text="EXIT", font="Arial 16", width=8, height=2,
command=self.controller.destroy)
exit_Btn.grid(row=2, column=0, columnspan=2, padx=30, pady=5)
def starting_Program(self):
pass
class counterPage(ttk.Frame): # Page to enter points for each team's event
def __init__(self, parent, controller):
self.controller = controller
ttk.Frame.__init__(self, parent)
self.userEntry()
def userEntry(self):
team1label = Label(self, text="Enter points for Team 1: ", font="Arial 20")
team1label.grid(row=0, column=0, pady=10, padx=10)
team1Points1 = Entry(self, width=2)
team1Points1.grid(row=0, column=1)
team1Points2 = Entry(self, width=2)
team1Points2.grid(row=0, column=2)
team1Points3 = Entry(self, width=2)
team1Points3.grid(row=0, column=3)
team1Points4 = Entry(self, width=2)
team1Points4.grid(row=0, column=4)
team1Points5 = Entry(self, width=2)
team1Points5.grid(row=0, column=5)
pointsTeam1 = team1Points1.get()
pointsTeam1 = team1Points2.get()
pointsTeam1 = team1Points3.get()
pointsTeam1 = team1Points4.get()
pointsTeam1 = team1Points5.get()
intConvert1 = int(pointsTeam1)
team2label = Label(self, text="Enter points for Team 2: ", font="Arial 20")
team2label.grid(row=1, column=0, pady=10, padx=10)
team2Points1 = Entry(self, width=2)
team2Points1.grid(row=1, column=1)
team2Points2 = Entry(self, width=2)
team2Points2.grid(row=1, column=2)
team2Points3 = Entry(self, width=2)
team2Points3.grid(row=1, column=3)
team2Points4 = Entry(self, width=2)
team2Points4.grid(row=1, column=4)
team2Points5 = Entry(self, width=2)
team2Points5.grid(row=1, column=5)
pointsTeam2 = team1Points1.get()
pointsTeam2 = team1Points2.get()
pointsTeam2 = team1Points3.get()
pointsTeam2 = team1Points4.get()
pointsTeam2 = team1Points5.get()
intConvert2 = int(pointsTeam2)
team3label = Label(self, text="Enter points for Team 3: ", font="Arial 20")
team3label.grid(row=2, column=0, pady=10, padx=10)
team3Points1 = Entry(self, width=2)
team3Points1.grid(row=2, column=1)
team3Points2 = Entry(self, width=2)
team3Points2.grid(row=2, column=2)
team3Points3 = Entry(self, width=2)
team3Points3.grid(row=2, column=3)
team3Points4 = Entry(self, width=2)
team3Points4.grid(row=2, column=4)
team3Points5 = Entry(self, width=2)
team3Points5.grid(row=2, column=5)
pointsTeam3 = team1Points1.get()
pointsTeam3 = team1Points2.get()
pointsTeam3 = team1Points3.get()
pointsTeam3 = team1Points4.get()
pointsTeam3 = team1Points5.get()
intConvert3 = int(pointsTeam3)
team4label = Label(self, text="Enter points for Team 4: ", font="Arial 20")
team4label.grid(row=3, column=0, pady=10, padx=10)
team4Points1 = Entry(self, width=2)
team4Points1.grid(row=3, column=1)
team4Points2 = Entry(self, width=2)
team4Points2.grid(row=3, column=2)
team4Points3 = Entry(self, width=2)
team4Points3.grid(row=3, column=3)
team4Points4 = Entry(self, width=2)
team4Points4.grid(row=3, column=4)
team4Points5 = Entry(self, width=2)
team4Points5.grid(row=3, column=5)
pointsTeam4 = team1Points1.get()
pointsTeam4 = team1Points2.get()
pointsTeam4 = team1Points3.get()
pointsTeam4 = team1Points4.get()
pointsTeam4 = team1Points5.get()
intConvert4 = int(pointsTeam4)
sum(pointsTeam1)
sum(pointsTeam2)
sum(pointsTeam3)
sum(pointsTeam4)
backBtn = Button(self, text="BACK", font="Arial 16", height=2, width=6,
command=lambda: self.controller.show_frame(StartPage))
backBtn.grid(row=4, column=0, sticky=W, pady=245, padx=10)
resultBtn = Button(self, text="Show Results", font="Arial 16", height=2, width=8,
command=lambda: print(intConvert1, intConvert2, intConvert3, intConvert4))
resultBtn.grid(row=4, column=0, sticky=W, pady=245, padx=100)
if __name__ == '__main__':
pointsTeam1 = []
pointsTeam2 = []
pointsTeam3 = []
pointsTeam4 = []
app = CollegeApp()
app.geometry("800x500")
app.resizable(False, False)
app.title('Points Counter')
app.mainloop()
AS Your code is written, it do this: When it runs, it tries to get the values on the Entry objects, but there is nothing, so thats the error, you trying to convert to int a ''. The correct thing to do is this:
from tkinter import *
import tkinter.ttk as ttk
# global var
class CollegeApp(Tk):
def __init__(self):
Tk.__init__(self)
container = ttk.Frame(self)
container.pack(side="top", fill="both", expand=True)
self.frames = {}
for F in (StartPage, counterPage):
frame = F(container, self)
self.frames[F] = frame
frame.grid(row=0, column=0, sticky="nsew")
self.show_frame(StartPage)
self.lift()
def show_frame(self, cont):
frame = self.frames[cont]
frame.tkraise()
class StartPage(ttk.Frame):
def __init__(self, parent, controller):
self.controller = controller
ttk.Frame.__init__(self, parent)
self.startMenu()
def startMenu(self):
heading = Label(self, text="College Tournament Points\n Count Software",
font=('Arial', 25))
heading.grid(row=0, column=0, columnspan=2, padx=240, pady=40)
start_Btn = Button(self, text="START", font="Arial 16", width=8, height=2,
command=lambda: self.controller.show_frame(counterPage))
start_Btn.grid(row=1, column=0, columnspan=2, padx=30, pady=5)
exit_Btn = Button(self, text="EXIT", font="Arial 16", width=8, height=2,
command=self.controller.destroy)
exit_Btn.grid(row=2, column=0, columnspan=2, padx=30, pady=5)
def starting_Program(self):
pass
class counterPage(ttk.Frame): # Page to enter points for each team's event
def __init__(self, parent, controller):
self.controller = controller
ttk.Frame.__init__(self, parent)
self.userEntry()
def get_values(self):
"""
get values of every list
"""
def get_if_int(value):
# check if value is numeric and return int value else return 0
if value.isnumeric():
return int(value)
return 0 # or raise ValueError
pointsTeam1 = []
pointsTeam1.append(get_if_int(self.team1Points1.get()))
pointsTeam1.append(get_if_int(self.team1Points2.get()))
pointsTeam1.append(get_if_int(self.team1Points3.get()))
pointsTeam1.append(get_if_int(self.team1Points4.get()))
pointsTeam1.append(get_if_int(self.team1Points5.get()))
print(sum(pointsTeam1))
pointsTeam2 = []
pointsTeam2.append(get_if_int(self.team2Points1.get()))
pointsTeam2.append(get_if_int(self.team2Points2.get()))
pointsTeam2.append(get_if_int(self.team2Points3.get()))
pointsTeam2.append(get_if_int(self.team2Points4.get()))
pointsTeam2.append(get_if_int(self.team2Points5.get()))
print(sum(pointsTeam2))
def userEntry(self):
team1label = Label(self, text="Enter points for Team 1: ", font="Arial 20")
team1label.grid(row=0, column=0, pady=10, padx=10)
# using self. to keep track of attributes
self.team1Points1 = Entry(self, width=2)
self.team1Points1.grid(row=0, column=1)
self.team1Points2 = Entry(self, width=2)
self.team1Points2.grid(row=0, column=2)
self.team1Points3 = Entry(self, width=2)
self.team1Points3.grid(row=0, column=3)
self.team1Points4 = Entry(self, width=2)
self.team1Points4.grid(row=0, column=4)
self.team1Points5 = Entry(self, width=2)
self.team1Points5.grid(row=0, column=5)
team2label = Label(self, text="Enter points for Team 2: ", font="Arial 20")
team2label.grid(row=1, column=0, pady=10, padx=10)
self.team2Points1 = Entry(self, width=2)
self.team2Points1.grid(row=1, column=1)
self.team2Points2 = Entry(self, width=2)
self.team2Points2.grid(row=1, column=2)
self.team2Points3 = Entry(self, width=2)
self.team2Points3.grid(row=1, column=3)
self.team2Points4 = Entry(self, width=2)
self.team2Points4.grid(row=1, column=4)
self.team2Points5 = Entry(self, width=2)
self.team2Points5.grid(row=1, column=5)
# pointsTeam2 = team1Points1.get()
# pointsTeam2 = team1Points2.get()
# pointsTeam2 = team1Points3.get()
# pointsTeam2 = team1Points4.get()
# pointsTeam2 = team1Points5.get()
intConvert2 = pointsTeam2
team3label = Label(self, text="Enter points for Team 3: ", font="Arial 20")
team3label.grid(row=2, column=0, pady=10, padx=10)
team3Points1 = Entry(self, width=2)
team3Points1.grid(row=2, column=1)
team3Points2 = Entry(self, width=2)
team3Points2.grid(row=2, column=2)
team3Points3 = Entry(self, width=2)
team3Points3.grid(row=2, column=3)
team3Points4 = Entry(self, width=2)
team3Points4.grid(row=2, column=4)
team3Points5 = Entry(self, width=2)
team3Points5.grid(row=2, column=5)
# pointsTeam3 = team1Points1.get()
# pointsTeam3 = team1Points2.get()
# pointsTeam3 = team1Points3.get()
# pointsTeam3 = team1Points4.get()
# pointsTeam3 = team1Points5.get()
intConvert3 = pointsTeam3
team4label = Label(self, text="Enter points for Team 4: ", font="Arial 20")
team4label.grid(row=3, column=0, pady=10, padx=10)
team4Points1 = Entry(self, width=2)
team4Points1.grid(row=3, column=1)
team4Points2 = Entry(self, width=2)
team4Points2.grid(row=3, column=2)
team4Points3 = Entry(self, width=2)
team4Points3.grid(row=3, column=3)
team4Points4 = Entry(self, width=2)
team4Points4.grid(row=3, column=4)
team4Points5 = Entry(self, width=2)
team4Points5.grid(row=3, column=5)
# pointsTeam4 = team1Points1.get()
# pointsTeam4 = team1Points2.get()
# pointsTeam4 = team1Points3.get()
# pointsTeam4 = team1Points4.get()
# pointsTeam4 = team1Points5.get()
intConvert4 = pointsTeam4
intConvert1 = sum(pointsTeam1)
sum(pointsTeam2)
sum(pointsTeam3)
sum(pointsTeam4)
backBtn = Button(self, text="BACK", font="Arial 16", height=2, width=6,
command=lambda: self.controller.show_frame(StartPage))
backBtn.grid(row=4, column=0, sticky=W, pady=245, padx=10)
# new function added: get_values()
resultBtn = Button(self, text="Show Results", font="Arial 16", height=2, width=8,
command=lambda: self.get_values())
resultBtn.grid(row=4, column=0, sticky=W, pady=245, padx=100)
if __name__ == '__main__':
pointsTeam1 = []
pointsTeam2 = []
pointsTeam3 = []
pointsTeam4 = []
app = CollegeApp()
app.geometry("800x500")
app.resizable(False, False)
app.title('Points Counter')
app.mainloop()
There are some things to change, you can see them with comments, you are using a list, so, for use the sum() function you need to .append()
the value on the list, and every time that you call the function reset the list with pointsTeam1 = []
Use the self.
to keep track of the attribute and get the value on the new function
get_values()
And for last thing, I made a function to check if the value inserted is a numeric value and convert it to int else return 0, you can raise an Error or show some message
I hope I was helpful