Tkinter Label()计数器变量

2024-04-26 22:49:19 发布

您现在位置:Python中文网/ 问答频道 /正文

我目前正在做一个游戏,金特圭岩,布,剪刀游戏。我希望能够计算并存储用户、计算机和领带的胜算。我已经能够设置一个IntVar()textvariable来与标签相关联,并且每当用户获胜时,我都会对其进行计数。但唯一的问题是,它只计算一个,例如(用户绘制“岩石”和计算机绘制“剪刀”[我们知道用户获胜,]但下次用户绘制石头,计算机绘制剪刀时,计数会降到0)。它只向上计数一次并重置,不转到下一个整数(因此它从0变为1,然后返回到0,而不是2)。有人能帮我做一个成功的计数器变量来计算胜负和平局。在

代码:

#Written by : Pamal Mangat.
#Written on : Monday, July 27th, 2015.
#Rock Paper Scissors : Version 1.2 (Tkinter [GUI] addition)

from tkinter import *
from sys import *
from PIL import Image, ImageTk
import pygame as py
import os
from random import randrange

py.init()

#Function runs the actual game. 
def runGame(startWindow):

    #Close [startWindow] before advancing:
    startWindow.destroy()
    startWindow.quit()

    master = Tk()
    master.title('Lets Play!')

    #Function carries on the remainder of the game.
    def carryGame(button_id):

        #Initial Values
        userWins = 0
        ties = 0
        computerWins = 0

        label_one = Label(master, text='Player Wins: ', font='Helvetica 11 bold', bg='PeachPuff2')
        label_one.place(x=15, y=210)

        label_two = Label(master, text='Ties: ', font='Helvetica 11 bold', bg='PeachPuff2')
        label_two.place(x=195, y=210)

        label_three = Label(master, text='Computer Wins: ', font='Helvetica 11 bold', bg='PeachPuff2')
        label_three.place(x=295, y=210)

        userWin_Text = IntVar()
        userWin_Text.set(userWins)

        ties_Text = IntVar()
        ties_Text.set(ties)

        computerWin_Text = IntVar()
        computerWin_Text.set(computerWins)

        userWin_Label = Label(master, textvariable = userWin_Text, font='Helvetica 10 bold', bg='PeachPuff2')
        userWin_Label.place(x=110, y=210)

        ties_Label = Label(master, textvariable = ties_Text, font='Helvetica 10 bold', bg='PeachPuff2')
        ties_Label.place(x=240, y=210)

        computerWin_Label = Label(master, textvariable = computerWin_Text, font='Helvetica 10 bold', bg='PeachPuff2')
        computerWin_Label.place(x=420, y=210)

        result = StringVar()
        printResult = Label(master, textvariable = result, font='Bizon 32 bold', bg='PeachPuff2')
        printResult.place(x=130, y=300)

        #Computer's move:
        random_Num = randrange(1,4)

        if random_Num == 1:
           computer_Move = 'Rock     '
           result.set(computer_Move)

        elif random_Num == 2:
            computer_Move = 'Paper     '
            result.set(computer_Move)

        else:
            computer_Move = 'Scissors'
            result.set(computer_Move)

        if button_id == 1:
            player_Move = 'Rock'

        elif button_id == 2:
            player_Move = 'Paper'

        else:
            player_Move = 'Scissors'

        if player_Move == 'Rock' and computer_Move == 'Scissors':
            userWins += 1
            userWin_Text.set(userWins)


    #Rock button
    rock_Button = Button(master, width=15, height=7, command=lambda:carryGame(1))
    rock_photo=PhotoImage(file=r'C:\Users\Pamal\Desktop\Documents\Python Folder\Python Projects\Rock, Paper, Scissors\V 1.2\Images\rock.png')
    rock_Button.config(image=rock_photo,width="120",height="120")
    rock_Button.place(x=17, y=70)

    #Paper button
    paper_Button = Button(master, width=15, height=7, command=lambda:carryGame(2))
    paper_photo=PhotoImage(file=r'C:\Users\Pamal\Desktop\Documents\Python Folder\Python Projects\Rock, Paper, Scissors\V 1.2\Images\paper.png')
    paper_Button.config(image=paper_photo,width="120",height="120")
    paper_Button.place(x=167, y=70)

    #Scissors button
    scissors_Button = Button(master, width=15, height=7, command=lambda:carryGame(3))
    scissors_photo=PhotoImage(file=r'C:\Users\Pamal\Desktop\Documents\Python Folder\Python Projects\Rock, Paper, Scissors\V 1.2\Images\scissors.png')
    scissors_Button.config(image=scissors_photo,width="120",height="120")
    scissors_Button.place(x=317, y=70)

    label_1 = Label(master, text='Please make your selection-', font='Bizon 20 bold', bg='PeachPuff2')
    label_1.pack(side=TOP)

    label_2 = Label(master, text='The computer picked:', font='Helvetica 22 bold', bg='PeachPuff2')
    label_2.place(x=70, y=240)

    #Locks window size
    master.maxsize(450, 400)
    master.minsize(450, 400)

    #Sets window background to PeachPuff2
    master.config(background='PeachPuff2')

    master.mainloop()

def startScreen():

    #Plays music for the application
    def playMusic(fileName):
        py.mixer.music.load(fileName)
        py.mixer.music.play()

    #Start Window
    startWindow = Tk()
    startWindow.title('[Rock] [Paper] [Scissors]')

    #Imports image as title
    load = Image.open(r'C:\Users\Pamal\Desktop\Documents\Python Folder\Python Projects\Rock, Paper, Scissors\V 1.2\Images\title.png')
    render = ImageTk.PhotoImage(load)
    img = Label(startWindow, image=render, bd=0)
    img.image = render
    img.place(x=-100, y=-65)

    clickToPlay = Button(startWindow, text='Play!', width=8, font='Bizon 20 bold', bg='Black', fg='Yellow', relief=RIDGE, bd=0, command=lambda:runGame(startWindow))
    clickToPlay.place(x=75, y=125)

    #Credit
    authorName = Label(startWindow, text='Written by : Pamal Mangat', font='Times 6 bold', bg='Black', fg='Yellow')
    authorName.place(x=2, y=230)

    versionNum = Label(startWindow, text='[V 1.2]', font='Times 6 bold', bg='Black', fg='Red')
    versionNum.place(x=268, y=230)

    #Start Screen Music
    playMusic(r'C:\Users\Pamal\Desktop\Documents\Python Folder\Python Projects\Rock, Paper, Scissors\V 1.2\Audio\title_Song.mp3')

    #Locks window size
    startWindow.maxsize(300, 250)
    startWindow.minsize(300, 250)

    #Sets window background to black
    startWindow.config(background='Black')

    startWindow.mainloop()

startScreen()

截图:

生成的窗口的图像: enter image description here


Tags: textmastermoveplacebuttonlabelpaperbg
1条回答
网友
1楼 · 发布于 2024-04-26 22:49:19

每次运行carryGame时都要初始化计数器变量。将它们提取到更高级别的作用域,以便只初始化一次:

编辑:试试这个

from tkinter import *
from sys import *
from PIL import Image, ImageTk
import pygame as py
import os
from random import randrange



#Initial Values
userWins = 0
ties = 0
computerWins = 0
py.init()


#Function runs the actual game. 
def runGame(startWindow):

    #Close [startWindow] before advancing:
    startWindow.destroy()
    startWindow.quit()

    master = Tk()
    master.title('Lets Play!')

    #Function carries on the remainder of the game.
    def carryGame(button_id):

        #Initial Values
        global userWins
        global ties
        global computerWins

        label_one = Label(master, text='Player Wins: ', font='Helvetica 11 bold', bg='PeachPuff2')
        label_one.place(x=15, y=210)

        label_two = Label(master, text='Ties: ', font='Helvetica 11 bold', bg='PeachPuff2')
        label_two.place(x=195, y=210)

        label_three = Label(master, text='Computer Wins: ', font='Helvetica 11 bold', bg='PeachPuff2')
        label_three.place(x=295, y=210)

        userWin_Text = IntVar()
        userWin_Text.set(userWins)

        ties_Text = IntVar()
        ties_Text.set(ties)

        computerWin_Text = IntVar()
        computerWin_Text.set(computerWins)

        userWin_Label = Label(master, textvariable = userWin_Text, font='Helvetica 10 bold', bg='PeachPuff2')
        userWin_Label.place(x=110, y=210)

        ties_Label = Label(master, textvariable = ties_Text, font='Helvetica 10 bold', bg='PeachPuff2')
        ties_Label.place(x=240, y=210)

        computerWin_Label = Label(master, textvariable = computerWin_Text, font='Helvetica 10 bold', bg='PeachPuff2')
        computerWin_Label.place(x=420, y=210)

        result = StringVar()
        printResult = Label(master, textvariable = result, font='Bizon 32 bold', bg='PeachPuff2')
        printResult.place(x=130, y=300)

        #Computer's move:
        random_Num = randrange(1,4)

        if random_Num == 1:
           computer_Move = 'Rock     '
           result.set(computer_Move)

        elif random_Num == 2:
            computer_Move = 'Paper     '
            result.set(computer_Move)

        else:
            computer_Move = 'Scissors'
            result.set(computer_Move)

        if button_id == 1:
            player_Move = 'Rock'

        elif button_id == 2:
            player_Move = 'Paper'

        else:
            player_Move = 'Scissors'

        if player_Move == 'Rock' and computer_Move == 'Scissors':
            userWins += 1
            userWin_Text.set(userWins)


    #Rock button
    rock_Button = Button(master, width=15, height=7, command=lambda:carryGame(1))
    rock_photo=PhotoImage(file=r'C:\Users\Pamal\Desktop\Documents\Python Folder\Python Projects\Rock, Paper, Scissors\V 1.2\Images\rock.png')
    rock_Button.config(image=rock_photo,width="120",height="120")
    rock_Button.place(x=17, y=70)

    #Paper button
    paper_Button = Button(master, width=15, height=7, command=lambda:carryGame(2))
    paper_photo=PhotoImage(file=r'C:\Users\Pamal\Desktop\Documents\Python Folder\Python Projects\Rock, Paper, Scissors\V 1.2\Images\paper.png')
    paper_Button.config(image=paper_photo,width="120",height="120")
    paper_Button.place(x=167, y=70)

    #Scissors button
    scissors_Button = Button(master, width=15, height=7, command=lambda:carryGame(3))
    scissors_photo=PhotoImage(file=r'C:\Users\Pamal\Desktop\Documents\Python Folder\Python Projects\Rock, Paper, Scissors\V 1.2\Images\scissors.png')
    scissors_Button.config(image=scissors_photo,width="120",height="120")
    scissors_Button.place(x=317, y=70)

    label_1 = Label(master, text='Please make your selection-', font='Bizon 20 bold', bg='PeachPuff2')
    label_1.pack(side=TOP)

    label_2 = Label(master, text='The computer picked:', font='Helvetica 22 bold', bg='PeachPuff2')
    label_2.place(x=70, y=240)

    #Locks window size
    master.maxsize(450, 400)
    master.minsize(450, 400)

    #Sets window background to PeachPuff2
    master.config(background='PeachPuff2')

    master.mainloop()

def startScreen():

    #Plays music for the application
    def playMusic(fileName):
        py.mixer.music.load(fileName)
        py.mixer.music.play()

    #Start Window
    startWindow = Tk()
    startWindow.title('[Rock] [Paper] [Scissors]')

    #Imports image as title
    load = Image.open(r'C:\Users\Pamal\Desktop\Documents\Python Folder\Python Projects\Rock, Paper, Scissors\V 1.2\Images\title.png')
    render = ImageTk.PhotoImage(load)
    img = Label(startWindow, image=render, bd=0)
    img.image = render
    img.place(x=-100, y=-65)

    clickToPlay = Button(startWindow, text='Play!', width=8, font='Bizon 20 bold', bg='Black', fg='Yellow', relief=RIDGE, bd=0, command=lambda:runGame(startWindow))
    clickToPlay.place(x=75, y=125)

    #Credit
    authorName = Label(startWindow, text='Written by : Pamal Mangat', font='Times 6 bold', bg='Black', fg='Yellow')
    authorName.place(x=2, y=230)

    versionNum = Label(startWindow, text='[V 1.2]', font='Times 6 bold', bg='Black', fg='Red')
    versionNum.place(x=268, y=230)

    #Start Screen Music
    playMusic(r'C:\Users\Pamal\Desktop\Documents\Python Folder\Python Projects\Rock, Paper, Scissors\V 1.2\Audio\title_Song.mp3')

    #Locks window size
    startWindow.maxsize(300, 250)
    startWindow.minsize(300, 250)

    #Sets window background to black
    startWindow.config(background='Black')

    startWindow.mainloop()

startScreen()

下面是正在执行的代码的图像: (正确计数) enter image description here

但一旦执行不同的命令(按下diff.按钮),计数就会变为0。 enter image description here

但如果用户再次获胜,则计数重新出现。 enter image description here

那么,我如何将号码一直显示在屏幕上??在

相关问题 更多 >