简体   繁体   English

Python - tkinter 按钮在执行第一个命令后不执行第二个命令

[英]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.在下面我的程序的简化版本中,我希望“显示结果”按钮(或代码中的resultPage按钮)首先总结用户输入的点,然后将他带到新页面。 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.但是,当我添加一个 function 将检查计算是否已处理并将用户带到新页面(代码中的def confirm )时,没有任何反应,我可以看到正在计算但程序没有打开新页面。

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.错误RecursionError: maximum recursion depth exceeded ,因为我无意中将 function def confirm到 function def get_values的内容翻了一番。 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:在您的confirm方法中,您有这一行:

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 .但是,您的get_values方法没有任何return语句,因此条件将始终失败,因为get_values将始终返回None So first, add some kind of return statement to pass the if block.所以首先,添加某种返回语句来传递if块。

Also, this line in CollegeAPP 's constructor method:此外, CollegeAPP的构造方法中的这一行:

for F in (StartPage, counterPage):

Should also contanin your resultPage class if this is the method you want to do it.如果这是您想要执行的方法,还应该包含您的resultPage class。 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.代码中还有很多需要修复的地方,但希望这可以帮助您获得想要的工作。

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

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