Tkinker文字输入进行投注

时间:2018-08-07 21:17:47

标签: python user-interface tkinter textinput

我正在提出允许用户输入10到100个硬币之间的赌注的另一种想法。然后需要将其保存到GUI的下一页。我已经尝试了一些代码,但是我什么也无法覆盖那里的内容,也没有尝试删除它并添加代码以供用户输入他们想下多少赌注。可以将这样的代码放入一个类中,以使其与其他代码有所不同吗?

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()
root.geometry("700x500")
root.title("Slot Machine")
root.configure(background='seagreen')

INIT_STAKE = 100
ITEMS = ["OCELOT", "MACAW", "JAGUAR", "IBIS", "CAIMAN", "BOA", "SCORPION", "TAPIR", "CONDOR", "BAMBOO", "FROG"]

first = None
second = None
third = None
stake = INIT_STAKE

nameLabel = tk.Label(root, text="PANTHER DEN", font=('Cambria', 60))
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():
    global first, second, third
    first = spin()
    second = spin()
    third = spin()
    score()

def quit_play():
    lbl.config(text="Game has ended. You won a total of " + str(stake) + " coins")

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

def score():
    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)


tk.Button(root, text="Play", command=play).pack()
tk.Button(root, text="Quit", command=quit_play).pack()
tk.Button(root, text="Exit", command=quit).pack()

root.mainloop()

1 个答案:

答案 0 :(得分:0)

您可以使用以下内容:

from tkinter import *
import random

class RollMachine(Frame):
    def __init__(self, parent=None):
    Frame.__init__(self, parent)
    self.first = self.second = self.third = None
    self.stake = 0
    self.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!"""
    self.ITEMS = ["OCELOT", "MACAW", "JAGUAR", "IBIS", "CAIMAN", "BOA", "SCORPION", "TAPIR", "CONDOR", "BAMBOO", "FROG"]

    self.makeWidgets()
    self.pack()

def makeWidgets(self):
    nameLabel = Label(self, text="PANTHER DEN", font=('Cambria', 60))
    nameLabel.pack()
    self.lbl = Label(self, text=self.intro, background='seagreen', font=('Cambria', 12))
    self.lbl.pack()
    self.lbl2 = Label(self, text="Enter the stake")
    self.lbl2.pack()
    self.stake_input = Entry(self)
    self.stake_input.pack()
    Button(self, text="Play", command=self.play).pack()
    Button(self, text="Quit", command=self.quit_play).pack()
    Button(self, text="Exit", command=self.quit).pack()

def play(self):
            if self.stake == 0:
                self.stake = int(self.stake_input.get())
            self.first = self.spin()
            self.second = self.spin()
            self.third = self.spin()
            self.score()

def spin(self):
    randomnumber = random.randint(0, 10)
    return self.ITEMS[randomnumber]

def score(self):
    if((self.first == "OCELOT") and (self.second != "MACAW")):
        win = 5
    elif((self.first == "JAGUAR") and (self.second == "JAGUAR") and (self.third != "JAGUAR")):
        win = 8 
    elif((self.first == "BOA") and (self.second == "BOA") and (self.third == "BOA")):
        win = 10
    elif((self.first == "CAIMAN") and (self.second == "CAIMAN") and ((self.third == "CAIMAN") or (self.third == "BOA"))):
        win = 8 
    elif((self.first == "MACAW") and (self.second == "IBIS") and ((self.third == "MACAW"))):
        win = 15 
    elif((self.first == "TAPIR") and (self.second == "TAPIR") and ((self.third == "TAPIR"))):
        win = 20 
    elif((self.first == "IBIS") and (self.second == "IBIS") and (self.third == "IBIS")):
        win = 300 
    elif((self.first == "SCORPION") and (self.second == "SCORPION") and (self.third == "SCORPION")):
        win = -500 
    else:
        win = -1 

    self.stake += win

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

def quit_play(self):
    self.lbl.config(text="Game has ended. You won a total of " + str(self.stake) + " coins")




root = Tk()
RollMachine(root)