python GUI关闭一帧并打开另一帧

时间:2013-05-06 14:08:15

标签: python user-interface tkinter

我正在编写一个程序,为曲棍球队使用两个班级(团队班和联赛班级)。 GUI正在模拟8支球队的单轮淘汰赛“季后赛”。我希望GUI允许用户输入第一轮4个游戏的分数,然后单击提交按钮。按下此按钮时,我想要弹出一个列出获胜者的消息框,然后当该框关闭时,将关闭原始框架,并弹出第2轮输入字段。我真的不确定如何做到这一点。 我有很多代码,所以我没有包含团队和联赛课程的代码,因为我不认为这个问题是必要的,但如果有人想要我可以发布。

所以基本上,有没有办法在框架上关闭/通过点击按钮将框架的外观更改为新框架?

#methods to set the scores for round 1
def round1game1team1Score(self):
    """set the score for team 1"""
    team1= self.l.getTeambyPosition(0)
    teamScore= self.round1game1team1Var.get()
    team1.setScore(teamScore)

def round1game1team8Score(self):
    """sets the score for team 8"""
    team8= self.l.getTeambyPosition(7)
    teamScore= self.round1game1team8Var.get()
    team8.setScore(teamScore)

def round1game2team2Score(self):
    """sets the score for team 2"""
    team2= self.l.getTeambyPosition(1)
    teamScore= self.round1game2team2Var.get()
    team2.setScore(teamScore)

def round1game2team7Score(self):
    """sets the score for team 7"""
    team7= self.l.getTeambyPosition(6)
    teamScore= self.round1game2team7Var.get()
    team7.setScore(teamScore)

def round1game3team3Score(self):
    """sets the score for team 3"""
    team3= self.l.getTeambyPosition(2)
    teamScore= self.round1game3team3Var.get()
    team3.setScore(teamScore)

def round1game3team6Score(self):
    team6= self.l.getTeambyPosition(5)
    teamScore= self.round1game3team6Var.get()
    team6.setScore(teamScore)

def round1game4team4Score(self):
    """sets the score for team 4"""
    team4= self.l.getTeambyPosition(3)
    teamScore= self.round1game4team4Var.get()
    team4.setScore(teamScore)

def round1game4team5Score(self):
    """sets the score for team 5"""
    team5= self.l.getTeambyPosition(4)
    teamScore= self.round1game4team5Var.get()
    team5.setScore(teamScore)


#methods that determine the winners in round 1
def round1game1Winner(self):
    """determines the winner of round 1 game 1"""
    team1= self.l.getTeambyPosition(0)
    team8= self.l.getTeambyPosition(7)
    if team1.getScore() > team8.getScore():
        return team1.getTeam()
    else:
        return team8.getTeam()

def round1game2Winner(self):
    """determines the winner of round 1 game 2"""
    team2= self.l.getTeambyPosition(1)
    team7= self.l.getTeambyPosition(6)
    if team2.getScore() > team7.getScore():
        return team2.getTeam()
    else:
        return team7.getTeam()

def round1game3Winner(self):
    """determines the winner of round 1 game 3"""
    team3= self.l.getTeambyPosition(2)
    team6= self.l.getTeambyPosition(5)
    if team3.getScore() > team6.getScore():
        return team3.getTeam()
    else:
        return team6.getTeam()

def round1game4Winner(self):
    """determines the winner of round 1 game 4"""
    team4= self.l.getTeambyPosition(3)
    team5= self.l.getTeambyPosition(4)
    if team4.getScore() > team5.getScore():
        return team4.getTeam()
    else:
        return team5.getTeam()

def closeRound1Frame(self):
    """closes round1 frame"""
    self.Destroy()



class Round1Frame(Frame):
    """GUI for round1 of the playoffs"""
    def __init__(self):
        """sets up the window and widgets"""
        Frame.__init__(self)
        self.master.title("2013 Stanley Cup Playoffs Round 1")
        self.grid()
        self.l= League()

        #round 1 game 1
        #team 1 label and entry field
        team1= self.l.getTeambyPosition(0).getTeam()
        round1game1team1Label= Label(self, text= str(team1) + '   ' + 'vs.' )
        round1game1team1Label.grid(row = 0, column = 0)
        self.round1game1team1Var = IntVar()
        round1game1team1Entry= Entry(self,
                                     textvariable= self.round1game1team1Var)
        round1game1team1Entry.grid(row = 1, column = 0)

        #team 8 label and entry field
        team8= self.l.getTeambyPosition(7).getTeam()
        round1game1team8Label= Label(self, text= str(team8))
        round1game1team8Label.grid(row = 0, column = 1)
        self.round1game1team8Var= IntVar()
        round1game1team8Entry= Entry(self,
                                     textvariable= self.round1game1team8Var)
        round1game1team8Entry.grid(row = 1, column = 1)


        #round 1 game 2
        #team 2 label and entry field
        team2= self.l.getTeambyPosition(1).getTeam()
        round1game2team2Label= Label(self, text= str(team2) + '   ' + 'vs.')
        round1game2team2Label.grid(row = 0, column = 2)
        self.round1game2team2Var= IntVar()
        round1game2team2Entry= Entry(self,
                                     textvariable= self.round1game2team2Var)
        round1game2team2Entry.grid(row = 1, column = 2)

        #team 7 label and entry field
        team7= self.l.getTeambyPosition(6).getTeam()
        round1game2team7Label= Label(self, text= str(team7))
        round1game2team7Label.grid(row = 0, column = 3)
        self.round1game2team7Var= IntVar()
        round1game2team7Entry= Entry(self,
                                     textvariable= self.round1game2team7Var)
        round1game2team7Entry.grid(row = 1, column = 3)


        #round 1 game 3
        #team 3 label and entry field
        team3= self.l.getTeambyPosition(2).getTeam()
        round1game3team3Label= Label(self, text= str(team3) + '   ' + 'vs.')
        round1game3team3Label.grid(row = 2, column = 0)
        self.round1game3team3Var= IntVar()
        round1game3team3Entry= Entry(self,
                                     textvariable= self.round1game3team3Var)
        round1game3team3Entry.grid(row = 3, column = 0)

        #team 6 label and entry field
        team6= self.l.getTeambyPosition(5).getTeam()
        round1game3team6Label= Label(self, text= str(team6))
        round1game3team6Label.grid(row = 2, column = 1)
        self.round1game3team6Var= IntVar()
        round1game3team6Entry= Entry(self,
                                     textvariable= self.round1game3team6Var)
        round1game3team6Entry.grid(row = 3, column = 1)


        #round 1 game 4
        #team 4 label and entry field
        team4= self.l.getTeambyPosition(3).getTeam()
        round1game4team4Label= Label(self, text= str(team4) + '   ' + 'vs.')
        round1game4team4Label.grid(row = 2, column = 2)
        self.round1game4team4Var= IntVar()
        round1game4team4Entry= Entry(self,
                                     textvariable= self.round1game4team4Var)
        round1game4team4Entry.grid(row = 3, column = 2)

        #team 5 label and field entry
        team5= self.l.getTeambyPosition(4).getTeam()
        round1game4team5Label= Label(self, text= str(team5))
        round1game4team5Label.grid(row = 2, column = 3)
        self.round1game4team5Var= IntVar()
        round1game4team5Entry= Entry(self,
                                     textvariable= self.round1game4team5Var)
        round1game4team5Entry.grid(row = 3, column =3)

        #button to submit round 1 scores
        round1Button= Button(self, text= 'Submit Scores', command= self.round1)
        round1Button.grid(row = 4, column = 1, columnspan = 2)



    def round1(self):
        """event handler for the round1Button"""
        round1game1team1Score(self)
        round1game1team8Score(self)
        round1game2team2Score(self)
        round1game2team7Score(self)
        round1game3team3Score(self)
        round1game3team6Score(self)
        round1game4team4Score(self)
        round1game4team5Score(self)
        round1game1Winner(self)
        round1game2Winner(self)
        round1game3Winner(self)
        round1game4Winner(self)
        messagebox.showinfo(title= 'Round 1 Results',
                            message= 'Winners advancing to next round: ' + '\n' + str(round1game1Winner(self))
                            + '\n' + str(round1game2Winner(self)) + '\n' +
                            str(round1game3Winner(self)) + '\n' +
                            str(round1game4Winner(self)))
        closeRound1Frame(self)
        Round2Frame().mainloop()

class Round2Frame(Frame):
    """GUI for round 2 of the playoffs"""
    def __int__(self):
        """sets up the window and widgets"""
        Frame.__init__(self)
        self.master.title('2013 Stanley Cup Playoffs Round 2')
        self.grid()

        #round 2 game 1
        #first team label and entry field
        team1= round1game1Winner(self)
        round2game1team1Label= Label(self, text= str(team1) + 'vs.   ')
        round2game1team1Label.grid(row = 0, column = 0)
        self.round2game1team1Var= IntVar()
        round2game1team1Entry= Entry(self,
                                     textvariable= self.round2game1team1Var)
        round2game1team1Entry.grid(row = 1, column = 0)

        #second team label and entry field
        team2= round1game2Winner(self)
        round2game1team2Label= Label(self, text= str(team2))
        round2game1team2Label.grid(row = 0, column = 1)
        self.round2game1team2Var= IntVar()
        round2game1team2Entry= Entry(self,
                                     textvariable= self.round2game1team2Var)
        round2game1team2Entry.grid(row = 1, column = 1)


        #round 2 game 2
        #third team label and entry field
        team3= round1game3Winner(self)
        round2game2team3Label= Label(self, text= str(team3) + 'vs.   ')
        round2game2team3Label.grid(row = 0, column = 2)
        self.round2game2team3Var= IntVar()
        round2game2team3Entry= Entry(self,
                                     textvariable= self.round2game2team3Var)
        round2game2team3Entry.grid(row = 1, column = 2)

        #fourth team label and entry field
        team4= round1game4Winner(self)
        round2game2team4Label= Label(self, text= str(team4))
        round2game2team4Label.grid(row = 0, column = 3)
        self.round2game2team4Var= IntVar()
        round2game2team4Entry= Entry(self,
                                     textvariable= self.round2game2team4Var)
        round2game2team4Entry.grif(row = 1, column = 3)




def main():
    """instantiate and pop up the window"""
    Round1Frame().mainloop()

main()

1 个答案:

答案 0 :(得分:0)

是的,有一种方法可以改变框架看单击按钮的方式。您可以使用borderwidth方法随时设置创建框架(backgroundconfigure等)时可以设置的任何属性:

import Tkinter as tk
...
f = tk.Frame(..., background="green", ...)
...
f.configure(background="red")
...

至于“关闭”框架 - 框架本身不能“关闭”,但您可以使用destroy方法删除它们。这将破坏框架及其所有子窗口小部件:

f.destroy()