用于投注的 Tkinter 文本输入
Posted
技术标签:
【中文标题】用于投注的 Tkinter 文本输入【英文标题】:Tkinker text enter for bet 【发布时间】:2019-01-15 01:29:54 【问题描述】:我正在跟踪另一个想法,即允许用户输入 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】:你可以这样使用:
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)
【讨论】:
它部分不起作用。我可以输入一个数字(实际上您必须输入),但是当您继续开始滚动游戏时,它会运行原始硬币数量(100)而不是您输入的数字 您被要求这样做是为了指定赌注,硬币数量会根据赌注的大小而减少或增加。如果你需要改变初始硬币数量的值,那么这很简单,你只需尝试一下即可。 现在检查。修改后记下初始金额以上是关于用于投注的 Tkinter 文本输入的主要内容,如果未能解决你的问题,请参考以下文章