Mission Impossible
Mission Impossible

Reputation: 377

How can I put frames inside frames using tkinter?

I'm making a baseball program based on the strategy games produced by Avalon Hills from the 70s & 80s, and the last part is a gui. I've made all the code to run the game command line, and I've got the code to select lineups with a gui. I envision a 3by1 grid with a scoreboard on the first row, a text box displaying the result, out, home run, double play, etc., and the last row is divided between the pitcher and batter cards on the left side, and a frame of buttons. The frame will flip between an offence and defence frame. So first the defence frame comes up with options like pitching change, change position, and play ball. Play ball changes the frame to the offence options, which will be pinch hit, pinch run, steal, and so on. But how can I put the buttons inside a frame, then combine the player cards and buttons in another frame, and then add that to the main frame?

The DFrame & OFrame classes are inner classes (hence the "elf", not "self"). I've got the dynamic switching between the 2 Frames. My problem is breaking the DFrame main loop, it only plays the top of the first, and the self.roadOuts never increments. Here's what I've got:

while self.innings < 8.5 or self.homeScore == self.roadScore:
    self.roadOuts = 0
    while self.roadOuts < 3:
        self.dFrame.mainloop()

class DFrame(Frame):
    def __init__(elf, parent):
        Frame.__init__(elf)
        elf._playButton = Button(elf, text = 'Play Ball',
                                command = parent.oMenu)
        elf._playButton.grid(row = 0, column = 0)
        elf._pitchingButton = Button(elf, text = 'Pitching Change',
                                command = parent.pitchingChange)
        elf._pitchingButton.grid(row = 1, column = 0)
        elf._positionButton = Button(elf, text = 'Defensive Substitution',
                                command = parent.positionChange)
        elf._positionButton.grid(row = 0, column = 1)
        elf._alignButton = Button(elf, text = 'Change Positions',
                                command = parent.positionSwap)
        elf._alignButton.grid(row = 1, column = 1)
        elf._doubleButton = Button(elf, text = 'Double Switch',
                                command = parent.doubleSwitch)
        elf._doubleButton.grid(row = 2, column = 0)
        elf._walkButton = Button(elf, text = 'Intentional Walk',
                                command = parent.intentionalWalk)
        elf._walkButton.grid(row = 2, column = 1)
        elf._depthButton = Button(elf, text = 'Change Infield Depth',
                                command = parent.infieldDepth)
        elf._depthButton.grid(row = 3, column = 0)

class OFrame(Frame):
    def __init__(elf, parent):
        Frame.__init__(elf)
        elf._playButton = Button(elf, text = 'Play Ball',
                                command = parent.atBat)
        elf._playButton.grid(row = 0, column = 0)
        elf._pinchHitButton = Button(elf, text = 'Pinch Hit',
                                command = parent.pinchHit)
        elf._pinchHitButton.grid(row = 1, column = 0)
        elf._prfButton = Button(elf, text = 'Pinch Run First',
                                command = parent.pinchRunFirst)
        elf._prfButton.grid(row = 0, column = 1)
        elf._prsButton = Button(elf, text = 'Pinch Run Second',
                                command = parent.pinchRunSecond)
        elf._prsButton.grid(row = 1, column = 1)
        elf._prtButton = Button(elf, text = 'Pinch Run Third',
                                command = parent.pinchRunThird)
        elf._prtButton.grid(row = 2, column = 1)
        elf._stealButton = Button(elf, text = 'Steal',
                                command = parent.steal)
        elf._stealButton.grid(row = 2, column = 0)
        elf._bunt4HitButton = Button(elf, text = 'Bunt for a hit',
                                command = parent.bunt4AHit)
        elf._bunt4HitButton.grid(row = 3, column = 0)
        elf._hitNRunButton = Button(elf, text = 'Hit And Run',
                                command = parent.hitAndRun)
        elf._hitNRunButton.grid(row = 4, column = 0)
        elf._sacButton = Button(elf, text = 'Sacrifice',
                                command = parent.sacrifice)
        elf._sacButton.grid(row = 4, column = 1)
        elf._squeezeButton = Button(elf, text = 'Squeeze',
                                command = parent.squeeze)
        elf._squeezeButton.grid(row = 3, column = 1)

the next method is called when the DFrame "play ball" button is clicked, and it makes the OFrame.

def oMenu(self):
    self.dFrame.grid_forget()
    self.dFrame.destroy()
    self.oFrame = self.OFrame(self)
    self.oFrame.grid(row = 1, column = 1)
    self.oFrame.mainloop()

and at the end of an at bat, I have:

self.oFrame.grid_forget()
self.oFrame.destroy()
self.dFrame = self.DFrame(self)
self.dFrame.grid(row = 1, column = 1)

Upvotes: 2

Views: 26563

Answers (1)

mgilson
mgilson

Reputation: 310297

I'm not sure I understand your question. It seems like you know how to put one frame in another (It is really not much different than adding a Button to a frame -- or any other widget). I think what you're asking is how to dynamically switch which frame is being displayed at any given time.

You probably want the grid_forget method. using the play_ball button should cause the defense_frame to call it's grid_forget method, while re-gridding the the offense_frame. Of course, this would be pack_forget if you're using the pack geometry manager.

EDIT

Added a very rudimentary working example of the grid layout you described. It can probably be done a lot better, but this should get you started. (Particularly the switchOffenseDefense function and the switch_button button).

import Tkinter as tk

base=tk.Tk()  #this is the main frame
root=tk.Frame(base)  #Really this is not necessary -- the other widgets could be attached to "base", but I've added it to demonstrate putting a frame in a frame.
root.grid(row=0,column=0)
scoreboard=tk.Frame(root)
scoreboard.grid(row=0,column=0,columnspan=2)

###
#Code to add stuff to scoreboard ...
# e.g. 
###
scorestuff=tk.Label(scoreboard,text="Here is the scoreboard")
scorestuff.grid(row=0,column=0)
#End scoreboard

#Start cards.
cards=tk.Frame(root)
cards.grid(row=1,column=0)
###
# Code to add pitcher and batter cards
###
clabel=tk.Label(cards,text="Stuff to add cards here")
clabel.grid(row=0,column=0)
#end cards

#Offense/Defense frames....
offense=tk.Frame(root)
offense.grid(row=1,column=1)
offense.isgridded=True #Dynamically add "isgridded" attribute.
offense_label=tk.Label(offense,text="Offense is coolest")
offense_label.grid(row=0,column=0)

defense=tk.Frame(root)
defense.isgridded=False
defense_label=tk.Label(defense,text="Defense is coolest")
defense_label.grid(row=0,column=0)

def switchOffenseDefense():
    print "Called"
    if(offense.isgridded):
        offense.isgridded=False
        offense.grid_forget()
        defense.isgridded=True
        defense.grid(row=1,column=1)
     else:
        defense.isgridded=False
        defense.grid_forget()
        offense.isgridded=True
        offense.grid(row=1,column=1)


switch_button=tk.Button(root,text="Switch",command=switchOffenseDefense)
switch_button.grid(row=2,column=1)

root.mainloop()

Upvotes: 7

Related Questions