首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >为下注输入Tkinker文本

为下注输入Tkinker文本
EN

Stack Overflow用户
提问于 2018-08-08 05:17:47
回答 1查看 59关注 0票数 0

我正在追随另一个想法,允许用户输入10到100个硬币之间的赌注。然后需要将其保存到GUI的下一页。我试了几个代码,但我不能得到任何东西来覆盖那里,以及任何尝试删除它,并添加一个代码,让用户输入他们想要的赌注。可以将这样的代码放入一个类中,使其与其他代码不同吗?

代码语言:javascript
复制
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()
EN

回答 1

Stack Overflow用户

发布于 2018-08-08 10:01:32

您可以使用类似以下内容:

代码语言:javascript
复制
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)
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51735566

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档