2012-03-23 136 views
0

私はAvalon Hillsが70年代の& 80年代に制作した戦略ゲームに基づいて、最後の部分はGUIです。私はゲームのコマンドラインを実行するためのすべてのコードを作った、と私はguiでラインナップを選択するコードを持っている。私は最初の行にスコアボード、結果、アウト、ホームラン、ダブルプレーなどを表示するテキストボックスを持つ3by1のグリッドを想定し、最後の行は左側の投手カードとバッターカードに分けられます。ボタンのフレーム。フレームは攻撃フレームと防御フレームの間を移動します。まず、防御フレームには、投球の変更、ポジションの変更、ボールのプレーなどのオプションがあります。プレイボールは、フレームをピンチヒット、ピンチラン、盗みなどの攻撃オプションに変更します。しかし、どのようにボタンをフレームの中に入れてから、別のフレームのプレーヤーカードとボタンを組み合わせてメインフレームに追加するのですか?tkinterを使ってフレームをフレームの中に入れたいのですが?

答えて

4

ご質問が分かりません。 1つのフレームを別のフレームに配置する方法を知っているようです(フレームにボタンを追加すること、または他のウィジェットと大きく異なるわけではありません)。私はあなたが求めているのは、どのフレームがどのような時に動的に表示されているかを動的に切り替える方法だと思います。

おそらくgrid_forgetメソッドが必要です。 play_ballボタンを使用すると、defense_frameがgrid_forgetメソッドと呼ばれ、offense_frameを再グリッドします。もちろん、パックジオメトリマネージャを使用している場合はpack_forgetになります。

EDIT

はあなたが説明したグリッドレイアウトの非常に初歩的な作業例を追加しました。おそらくもっとうまくいくかもしれませんが、これはあなたを始めるはずです。 (特にswitchOffenseDefense関数とswitch_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() 
+0

はこれだけ、新しいフレームを初期化するにdefence_frameを投げ、選手カードのフレームにそのフレームを追加そのフレームをメインフレームに追加しますか? –

+0

@MissionImpossible:実際の例を追加しました。 – mgilson

+0

@ mgilson:私はフレームの切り替えを得ました。私の問題は今ではイニングです。あなたはおそらく知っているように、ロードチームは3アウトを得るまで打ち負かされ、ホームチームは3アウトを得るまで打ち負かす。これは9イニングの間続く。私 –

0

DFrame & OFrameクラスは内部クラス(したがって "ELF" ではなく "自己")です。私は2つのフレーム間の動的な切り替えを持っています。私の問題は、DFrameのメインループを壊しているだけで、最初のものの先頭を再生し、self.roadOutsは決して増加しません。

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) 

DFrame「プレイボール」ボタンをクリックすると、それが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() 

と打席の終わりに、私が持っている:mgilson @

self.oFrame.grid_forget() 
self.oFrame.destroy() 
self.dFrame = self.DFrame(self) 
self.dFrame.grid(row = 1, column = 1) 
関連する問題