简体   繁体   English

Python - 如何分配条目小部件中的数字以列出和汇总它们?

[英]Python - How to assign numbers from Entry Widgets to list and sum them?

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: ''我想知道,如何将它们分配给特定列表并将这些点相加以打印结果......我下面的代码显示了我尝试过的内容,但它不起作用并且我收到一个错误: ValueError: invalid literal for int() with base 10: ''

Also, if I will remove sum functions, numbers does not appear in lists.此外,如果我要删除 sum 函数,数字不会出现在列表中。 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 ''. AS 你的代码是这样写的:当它运行时,它试图获取 Entry 对象上的值,但是什么也没有,所以这就是错误,你试图转换为 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.有一些东西需要改变,你可以用评论看到它们,你正在使用一个列表,所以,为了使用 sum() function 你需要.append()列表上的值,每次你调用 function使用pointsTeam1 = [] 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跟踪属性并获取新 function get_values()上的值最后一件事,我做了一个 function 来检查插入的值是否为数值并将其转换为 int 否则返回 0,您可以引发错误或显示一些信息

I hope I was helpful我希望我有帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM