赞
踩
我目前正在开发Python[Tkinter]石头布剪刀GUI应用程序。到目前为止,我已经能够构建界面,甚至允许用户进行选择。唯一的问题是我不知道如何把电脑的决定打印出来。我想能够更新一个标签或上传一个图像,这取决于计算机选择了什么。例如,用户单击rock,我希望能够在窗口中打印一个岩石的图像,或单词rock,并在下次用户选择新选项(如剪刀或纸张)并重置时更新它。所以简单地说,我希望能够在用户每次单击按钮时创建一个更新的图像或/文本(标签)。
P、 我使用pygame的唯一原因是允许我的应用程序与音乐一起运行。我还提供了下面的应用程序截图。在
代码:#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):
result = StringVar()
printResult = Label(master, textvariable = result, font='Bizon 32 bold', bg='PeachPuff2')
printResult.place(x=150, y=300)
#Computer's move:
random_Num = randrange(1,4)
if random_Num == 1:
computer_Move = 'Rock'
elif random_Num == 2:
computer_Move = 'Paper'
else:
computer_Move = 'Scissors'
if button_id == 1:
player_Move = 'Rock'
elif button_id == 2:
player_Move = 'Paper'
else:
player_Move = 'Scissors'
#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()
截图:
启动屏幕的图像:
在开始屏幕上单击“播放”后打开的窗口的图像:
蓝色圆圈区域是我希望显示计算机决策的地方。在
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。