2016-12-23 19 views
0

まず最初に、ここではプログラムの背景をいくつか紹介します。Python Tkinterでウィンドウを更新

私はOOPとGUI開発を進めています。私は、以下のコードは素晴らしいことではないことを知っていますが、それはGUIとOOPでの私の最初の試みです。

私はそれをたくさんやっています(すべてが非常に非効率な方法です)が、私はプレーヤーに提供している情報に問題があります。

プレイヤーがゲームを進行するにつれて、私はプレイヤーが何をすべきかについてプログラムから促されたいと思っています。私はラベルと、機能とテキストウィジェットを持たないボタンでこれを試しました(これは完全な失敗でした)。

領土をオブジェクトとして作成し、それを使用してGUIボードをビルドするプログラムを入手できます。私は、ユーザーが開始地域を選択し、軍隊を追加できるようにすることができます。私は、攻撃する場所と攻撃する場所をユーザーが選択できるセクションを構築中です。これはすべてファッションに作用しています。

しかし、私の質問は... ボタンが機能によって更新されても、古い値が表示されています。最新のメッセージを表示したり、毎回新しいボタンを作成したりする方法はありますか?

from tkinter import * 
import random 

class territory: 
    def __init__ (self, country, player = 0, current_armies = 0, x=0, y=0, pos=0, neighbours=""): 
     self.country = country 
     self.current_armies = current_armies 
     self.player = player 
     self.y = y 
     self.x = x 
     self.pos = pos 
     self.neighbours = neighbours 


    def get_armies(self): 
     print(self.country + " has " + str(self.current_armies)+ " armies.") 

    def add_armies (self, armies): 
     self.current_armies += armies 

    def roll_dice (self, dice=1): 
     rolls = [] 
     for i in range(0, dice): 
      rolls.append(random.randint(1,6)) 
     rolls.sort() 
     rolls.reverse() 
     print (self.country + " has rolled " + str(rolls)) 
     return rolls 

    def owner(self): 
     print (self.country + " is owned by " + self.player) 

    def get_country(self): 
     print(country) 

def create_territories(): 
    countries = ["UK", "FRA", "SPA", "GER"] 
    terr_pos = [[0,1],[1,1],[1,2],[2,0]] 
    sta_arm = [1,1,1,1] 
    pos = [0,1,2,3] 
    neighb = [["FRA","SPA"],["UK","SPA","GER"],["FRA"],["FRA"]] 
    terr = [] 
    for i in range(len(countries)):  
     terr.append(territory(countries[i],0, sta_arm [i] , 
           terr_pos[i][0],terr_pos[i][1], pos[i], neighb[i]))  
    return terr 

## Button Commands 
def claim_t(territory, i): 
    global player1_reserves, player2_reserves, cur_player, claimed, title 

    if territory[i].player == 0: 
     territory[i].player = cur_player 
     claimed += 1 

     if cur_player == 1: 
      cur_player = 2 
     else: 
      cur_player = 1 
    else: 
     print("Teritory already claimed. Choose again") 
    if claimed == len(territory): 
     title = "Add Armies" 
     message = ("player " + str(cur_player) + " add army to one of your territories.") 
     army_board (territory) 
    else: 
     claim_board(territory) 

def add_army (territories, i): 
    global player1_reserves, player2_reserves, cur_player, title 


    if territories[i].player == cur_player: 

     if cur_player == 1: 
      if player1_reserves >0: 
       territories[i].current_armies += 1 
       player1_reserves -= 1 
       print(player1_reserves) 
       cur_player = 2 
      else: 
       print("You have no reserves left") 
       cur_player = 2 
     else: 
      if player2_reserves >0: 
       territories[i].current_armies += 1 
       player2_reserves -= 1 
       print(player2_reserves) 
       cur_player = 1 
      else: 
       print("You have no reserves left") 
       cur_player = 1 
     army_board (territories) 
    else: 
     print("Not your territory") 
    if player1_reserves == 0 and player2_reserves == 0: 
     cur_player = 1 
     play_board(territories) 
    else: 
     print ("Player " + str(cur_player) + 
       " add army to one of your territories.") 


def run_game (territories, i): 
    global attacker, defender, cur_player, attack_defend, message 
    if attack_defend == "attack": 
     attacker = i 

     message = str(cur_player) + " has chosen to attack from " +territories[i].country + ". Choose target country." 

     attack_defend = "defend" 

     play_board (territories) 
    else: 
     if territories[i].country in territories[attacker].neighbours: 
      message = "Valid Attack" 
      defender = i 
      attack_defend = "attack" 
      play_board(territories) 
     else: 
      message = "You can't attack " + territories[i].country + " from " + territories[attacker].country + " Choose again" 
      play_board(territories) 



## Board Builders 
def claim_board(territories): 
    global cur_player 
    buttonUk = Button(text = territories[0].country + " p= " + 
         str(territories[0].player), width = 10, 
         command=lambda: claim_t(territories, 0), 
         fg = "red").grid(row=territories[0].y,column=territories[0].x) 

    buttonFRA = Button(text = territories[1].country + " p= " + 
         str(territories[1].player), width = 10, 
         command=lambda: claim_t(territories, 1)).grid(row=territories[1].y,column=territories[1].x) 

    buttonSpa = Button(text = territories[2].country + " p= " + 
         str(territories[2].player), 
         width = 10, command=lambda: claim_t(territories, 2)).grid(row=territories[2].y,column=territories[2].x) 

    buttonGER = Button(text = territories[3].country + " p= " + 
         str(territories[3].player), width = 10, 
         command=lambda: claim_t(territories, 3)).grid(row=territories[3].y,column=territories[3].x) 

    label = Label (text = "Claim your territories").grid(row=4, column = 1) 

    label_1 = Label (text = "player " + str(cur_player) + 
        " add army to one of your territories.").grid(row=4, column = 1) 


def army_board (territories): 
    global cur_player, player1_reserves, player2_reserves 
    buttonUk = Button(text = territories[0].country+ 
         " a= "+str(territories[0].current_armies) + 
         " p= "+str(territories[0].player), width = 16, 
         command=lambda: add_army(territories, 0)).grid(row=territories[0].y,column=territories[0].x) 

    buttonFRA = Button(text = territories[1].country+ 
         " a= "+str(territories[1].current_armies)+ 
         " p= "+str(territories[1].player), width = 16, 
         command=lambda: add_army(territories, 1)).grid(row=territories[1].y,column=territories[1].x) 

    buttonSpa = Button(text = territories[2].country+ 
         " a= "+str(territories[2].current_armies)+ 
         " p= "+str(territories[2].player), width = 16, 
         command=lambda: add_army(territories, 2)).grid(row=territories[2].y,column=territories[2].x) 

    buttonGER = Button(text = territories[3].country+ 
         " a= "+str(territories[3].current_armies)+ 
         " p= "+str(territories[3].player), width = 16, 
         command=lambda: add_army(territories, 3)).grid(row=territories[3].y,column=territories[3].x) 

    label = Label (text = "Place your armies").grid(row=4, column = 1, columnspan = 4) 

    label = Label (text = "Player " + str(cur_player) + 
        "     place a reserve     ").grid(row=5, column = 1, columnspan = 5) 

    if cur_player == 1: 
     reserves = player1_reserves 
    else: 
     reserves = player2_reserves 

    label = Button (text = "Player " + str(cur_player) + 
        " you have " + str(reserves) + 
      " reserves to place").grid(row=5, column = 1, columnspan = 4) 

    print("Player " + str(cur_player) + 
        " you have " + str(reserves) + 
      " reserves to place") 


def play_board (territories): 
    global cur_player, attacker, defender, message 
    buttonUk = Button(text = territories[0].country+ 
         " a= "+str(territories[0].current_armies) + 
         " p= "+str(territories[0].player), 
         width = 16, command=lambda: run_game(territories, 0)).grid(row=territories[0].y,column=territories[0].x) 

    buttonFRA = Button(text = territories[1].country+ 
         " a= "+str(territories[1].current_armies)+ 
         " p= "+str(territories[1].player), width = 16, command=lambda: run_game(territories, 1)).grid(row=territories[1].y,column=territories[1].x) 

    buttonSpa = Button(text = territories[2].country+ 
         " a= "+str(territories[2].current_armies)+ 
         " p= "+str(territories[2].player), width = 16, command=lambda: run_game(territories, 2)).grid(row=territories[2].y,column=territories[2].x) 

    buttonGER = Button(text = territories[3].country+ 
         " a= "+str(territories[3].current_armies)+ 
         " p= "+str(territories[3].player), width = 16, command=lambda: run_game(territories, 3)).grid(row=territories[3].y,column=territories[3].x) 

    label = Label (text = "Now it is time to wage war").grid(row=4, column = 1) 

    label = Button (text = message).grid(row=5, column = 1) 
    print(message) 


##Game Sections 

def claim_territory (territory): 
    global claimed, title 
    window = Tk() 
    window.title ("Domination") 
    if claimed != len(territory): 
     claim_board(territory) 
    else: 
     window.destroy() 
    window.mainloop() 

## Global Variables 
territories = create_territories() 
cur_player = 1 
player1_reserves = 1 
player2_reserves = 1 
claimed = 0 
attacker = "" 
defender = "" 
message = "Player " + str(cur_player) + " Select a location to attack from" 
attack_defend = "attack" 


## Running of Game 

claim_territory(territories) 
+0

あなたのボタン格納変数は、それらを生成する関数に対してローカルである:ここでのドキュメントを見てください。機能が終了すると、ボタンは非表示になります。 – Marvo

+0

私はあなたがここで何を意味するのか理解していると思います。基本的には、各呼び出しでボタンが再作成され、古いものが上書きされることはありません。 私はウィンドウをクリアしてウィンドウを破壊しようとしましたが、何らかの理由でウィンドウを閉じるときにエラーが発生します。 私はこれを再度訪問し、各ボードをクラスとして構築して、毎回ボタンを編集できるようにしたいと考えています。 私は自分のウェブ検索で不足しているので、これに関するアドバイスを得るためのどこの考えやヒントに感謝します。 –

+1

[最小限の、完全で検証可能なサンプルの作成方法](http://www.stackoverflow.com/help/mcve)をお読みください。 –

答えて

関連する問題