Python-如何从Entry Widgets中分配数字以列出并求和



我创建了一些入口小部件,供用户输入不同团队的分数。我想知道,如何将它们分配到特定的列表中,并将这些点相加以打印结果。。。我下面的代码显示了我尝试过的内容,但它不起作用,我收到了一个错误: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 Pointsn 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 Pointsn 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,否则返回0,您可以引发错误或显示一些消息

我希望我对有所帮助

最新更新