簡體   English   中英

Tkinter Label() 計數器變量

[英]Tkinter Label() Counter Variable

我目前正在開發 Tkinter GUI 剪刀石頭布游戲。 我希望能夠計算和存儲用戶、計算機擁有的獲勝次數和關系數。 我已經能夠設置一個 IntVar() 文本變量來與標簽關聯,並且每次用戶獲勝時我都會計數。 但唯一的問題是,它只計算一個,例如(用戶畫“石頭”,電腦畫“剪刀”[我們知道用戶贏了,]但下次用戶畫石頭,電腦畫剪刀計數下降到 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()

截圖:

生成的窗口的圖像: 在此處輸入圖片說明

每次運行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()

這是正在執行的代碼的圖像:(它正確地進行計數) 在此處輸入圖片說明

但是一旦執行了不同的命令(按下差異按鈕),計數就會變為 0。 在此處輸入圖片說明

但如果用戶再次獲勝,則計數重新出現。 在此處輸入圖片說明

那么我如何始終在屏幕上保留號碼?

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM