添加修补程序输入框以在游戏GUI或游戏之前的GUI中运行

时间:2018-08-09 01:08:11

标签: python user-interface

好的,所以我想添加一个文本框(输入框),该文本框可以在它自己的窗口中运行,也可以在游戏之前的同一窗口中运行。用户将输入要在老虎机游戏中玩的名称。我一直在尝试使用此代码和其他一些代码,但是出现一些错误,或者它拒绝运行其余代码。有什么想法吗?

top = Tk()
L1 = Label(top, text="User Name")
L1.pack( side = LEFT)
E1 = Entry(top, bd =5)
E1.pack(side = RIGHT)

top.mainloop()

这是内部所需的代码

import tkinter as tk
import random

intro = """Panther's Den Slot Machine.


Welcome to my den!

You can win by rolling Ocelots, Jaguars, Boas, Caimans, Macaws or Tapirs.
You can also with big with three Ibis.

You'll lose a coin for anything else, and if you roll three Scorpions say good bye to 500 coins

Good luck kit!"""


root = tk.Tk() #Load the window
root.geometry("700x500") #Decides the window size. This can still be changed by the user once the game loads
root.title("Slot Machine") #This gives the window a name rather then it being "Patherden_v13.py"
root.configure(background='seagreen') #This gives the window a specific background color

INIT_STAKE = 100 #This is the amount of coins a player can start with
ITEMS = ["OCELOT", "MACAW", "JAGUAR", "IBIS", "CAIMAN", "BOA", "SCORPION", "TAPIR", "CONDOR", "BAMBOO", "FROG"]
#Above are all the different slot machine items that can be rolled

first = None #First/Second/Third are each of the tree slots that can be rolled
second = None
third = None
stake = INIT_STAKE #This refers back to the amount of coins the player starts with. Depending on what is rolled this will go up/down

nameLabel = tk.Label(root, text="PANTHER DEN", font=('Cambria', 60)) #The game's main title that loads first in the GUI
nameLabel.pack()
lbl = tk.Label(root, text=intro, background='seagreen', font=('Cambria', 12))
lbl.pack()
lbl2 = tk.Label(root, text=stake)
lbl2.pack()


def play(): #Rolls the first/second/third slots, refering back to the items avilable to roll
    global first, second, third
    first = spin()
    second = spin()
    third = spin()
    score()

def quit_play(): #This finished the game and tells the user how much they have
    lbl.config(text="Game has ended. You won a total of " + str(stake) + " coins")

def spin(): #This spins them
    randomnumber = random.randint(0, 10)
    return ITEMS[randomnumber]

def score(): #This has each of the combinations and how much can be won from rolling these
    global stake, first, second, third
    if((first == "OCELOT") and (second != "MACAW")):
        win = 5
    elif((first == "JAGUAR") and (second == "JAGUAR") and (third != "JAGUAR")):
        win = 8
    elif((first == "BOA") and (second == "BOA") and (third == "BOA")):
        win = 10
    elif((first == "CAIMAN") and (second == "CAIMAN") and ((third == "CAIMAN") or (third == "BOA"))):
        win = 8
    elif((first == "MACAW") and (second == "IBIS") and ((third == "MACAW"))):
        win = 15
    elif((first == "TAPIR") and (second == "TAPIR") and ((third == "TAPIR"))):
        win = 20
    elif((first == "IBIS") and (second == "IBIS") and (third == "IBIS")):
        win = 300
    elif((first == "SCORPION") and (second == "SCORPION") and (third == "SCORPION")):
        win = -500
    else:
        win = -1

    stake += win

    if(win > 0):
        lbl.config(text="{}\t{}\t{} -- You win {} Coins".format(first, second, third, win))
        lbl2.config(text=stake)
    else:
        lbl.config(text="{}\t{}\t{} -- You lose".format(first, second, third))
        lbl2.config(text=stake)

class but(): #This is a class the contains the three buttons players use
        tk.Button(root, text="Play", command=play).pack() #This button keeps rolling new slots for the player to try their luck
        tk.Button(root, text="Quit", command=quit_play).pack() #This button ends the game
        tk.Button(root, text="Exit", command=quit).pack() #This button closed the GUI winder


root.mainloop() #runs the entire lop of the game until the player quits uding the exit buton

0 个答案:

没有答案