Python - 如何从 Entry Widgets 分配数字以列出并求和?
Python - How to assign numbers from Entry Widgets to list and sum them?
我创建了一些输入小部件供用户输入不同团队的分数。我想知道,如何将它们分配给特定列表并将这些点相加以打印结果......
我下面的代码显示了我尝试过的方法,但它不起作用并且我收到错误消息:
ValueError: invalid literal for int() with base 10: ''
此外,如果我删除求和函数,数字将不会出现在列表中。谁能解释我如何解决这个问题?
我的代码:
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 你的代码是这样写的:
当它运行时,它会尝试获取 Entry 对象的值,但什么也没有,所以这就是错误,您试图将其转换为 int a ''。
正确的做法是:
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()
有些地方需要更改,您可以在注释中看到它们,您使用的是列表,因此,要使用 sum() 函数,您需要 .append()
列表中的值,每次调用该函数时都会用 pointsTeam1 = []
重置列表
使用 self.
跟踪属性并获取新函数的值
get_values()
最后,我创建了一个函数来检查插入的值是否为数值并将其转换为 int else return 0,您可以引发错误或显示一些消息
希望对您有所帮助
我创建了一些输入小部件供用户输入不同团队的分数。我想知道,如何将它们分配给特定列表并将这些点相加以打印结果......
我下面的代码显示了我尝试过的方法,但它不起作用并且我收到错误消息:
ValueError: invalid literal for int() with base 10: ''
此外,如果我删除求和函数,数字将不会出现在列表中。谁能解释我如何解决这个问题? 我的代码:
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 你的代码是这样写的: 当它运行时,它会尝试获取 Entry 对象的值,但什么也没有,所以这就是错误,您试图将其转换为 int a ''。 正确的做法是:
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()
有些地方需要更改,您可以在注释中看到它们,您使用的是列表,因此,要使用 sum() 函数,您需要 .append()
列表中的值,每次调用该函数时都会用 pointsTeam1 = []
重置列表
使用 self.
跟踪属性并获取新函数的值
get_values()
最后,我创建了一个函数来检查插入的值是否为数值并将其转换为 int else return 0,您可以引发错误或显示一些消息
希望对您有所帮助