繁体   English   中英

Python Tkinter中的更新窗口

[英]updating window in 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)

在实例化Button之后,可以使用Tkinkter库中Button类的config方法来更新任何一个按钮属性。 在这里查看文档:

http://effbot.org/tkinterbook/button.htm#Tkinter.Button.config-方法

暂无
暂无

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

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