Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/346.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在pythontkinter中更新窗口_Python_Python 3.x_Oop_User Interface_Tkinter - Fatal编程技术网

在pythontkinter中更新窗口

在pythontkinter中更新窗口,python,python-3.x,oop,user-interface,tkinter,Python,Python 3.x,Oop,User Interface,Tkinter,首先,这里是一些背景的程序 我正在尝试通过OOP和GUI开发来完成我的工作。我知道下面的代码不是很好,但这是我第一次尝试GUI和OOP 我已经得到了很多我想做的事情(可能都是以一种非常低效的方式),但是我对我提供给球员的信息有一个问题 当玩家在游戏中前进时,我希望程序提示他们应该做什么。我曾经尝试过使用标签、没有功能的按钮和文本小部件(这是一个完全的失败) 我可以让程序创建领土作为对象,然后使用它们构建GUI板。然后,我可以允许用户选择起始领土并添加军队。我正在创建一个部分,允许用户选择从何处进

首先,这里是一些背景的程序

我正在尝试通过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)

您可以使用
Tkinkter
库中
Button
类的
config
方法在实例化
按钮后更新任何一个按钮属性。请查看此处的文档:


存储变量的按钮是生成变量的函数的本地按钮。当功能结束时,按钮将无法使用。我想我理解你在这里的意思。基本上,每次调用时都会重新创建按钮,而不是覆盖旧按钮。我尝试过清除窗口并销毁窗口,但由于某种原因,我在尝试关闭窗口时出错。我将再次访问此页面,并尝试将每个板构建为一个类,我希望这将允许我每次编辑按钮。如果您有任何想法或建议,请向我咨询,我将不胜感激,因为我在网络搜索中遇到了一些问题。请阅读