简体   繁体   中英

Python - tkinter Button does not execute the second command, after executing the first command

In a simplified version of my program below, I wanted the "Show Result" button (or resultPage button in code) to first summarise the points entered by the user, and then bring him to a new page. However, when I added a function that will check that the calculations have been processed and take the user to a new page ( def confirm in code), nothing happens, I can see calculations being made but program does not open new page.

I would like to know how you can fix this error

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):
    def __init__(self, parent, controller):
        self.controller = controller
        ttk.Frame.__init__(self, parent)
        self.userEntry()

    def confirm(self):
        if self.get_values():
            self.controller.show_frame(resultPage)

    def get_values(self):
        def get_if_int(value):
            if value.isnumeric():
                return int(value)
            return 0

        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)

        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)

        resultBtn = Button(self, text="Show Results", font="Arial 16", height=2, width=8,
                           command=lambda: self.confirm())
        resultBtn.grid(row=4, column=0, sticky=W, pady=245, padx=100)


class resultPage(ttk.Frame):
    def __init__(self, parent, controller):
        self.controller = controller
        ttk.Frame.__init__(self, parent)
        self.userEntry()

    def userEntry(self):
        newlabel = Label(self, text="New Page", font="Arial 20")
        newlabel.grid(row=2, column=0, pady=10, padx=10)


if __name__ == '__main__':
    pointsTeam1 = []
    pointsTeam2 = []
    app = CollegeApp()
    app.geometry("800x500")
    app.resizable(False, False)
    app.title('Points Counter')
    app.mainloop()

UPDATE

Error RecursionError: maximum recursion depth exceeded raised because I have doubled the content of function def confirm to function def get_values inadvertently. The error no longer occurs, however my problem remains the same as the new one does not appear on a button press

In your confirm method, you have this line:

if self.get_values():

However, you don't have any return statement for your get_values method, so the condition will always fail as get_values will always return None . So first, add some kind of return statement to pass the if block.

Also, this line in CollegeAPP 's constructor method:

for F in (StartPage, counterPage):

Should also contanin your resultPage class if this is the method you want to do it. So:

for F in (StartPage, counterPage, resultPage):

There's plenty more to be fixed in the code but hopefully that helps you get what you want working.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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