我已经苦苦挣扎了两个星期,试图将这段代码加载到Tkinter GUI中。我想到的是,先加载欢迎屏幕,然后再在正常窗口中加载其余游戏会更容易。但是,我似乎无法弄清楚如何获取Tkinter GUI窗口来加载,让它成为游戏其余部分之前首先要加载的东西。我已经导入了tkinter,并且当我尝试使其仅显示在tkinter窗口中时,我也进行了随机导入,但是现在我放弃了。
在以前的工作中,我能够使以下tkinter代码正常工作以加载窗口,但我无法在其中加载它们,
root = tkinter.Tk()
root.title("Window Title")
root.geometry("500x300")
root.configure(background='seagreen')
下面是我正在研究的当前老虎机游戏代码中的内容。单独运行可以正常工作,但是一旦我开始添加以上代码或类似的代码,它就会中断。即使第一行正确地缩进,它也会在第一行的根开头出现一个错误,提示“意外缩进”
import tkinter
import random
print("Panther's Den Slot Machine. Please Answer with y/n")
print()
print()
print("Welcome to my den! You can win by rolling Ocelots, Jaguars, Boas, Caimans, Macaws or Tapirs. You can also with big with three Ibis.")
print()
print("You'll lose a coin for anything else, and if you roll three Scorpions say good bye to 500 coins")
print("Good luck kit!")
print()
print()
#create a window. set the title. set the size.
root = tkinter.Tk()
root.title("Slot Machine")
root.geometry("700x400")
#a function that will pick (and display) a name.
#def pickName():
# nameLabel.configure(text=random.choice(ITEMS))
#Displaying the name
nameLabel = tkinter.Label(root, text="PANTHER DEN", font=('Cambria', 60))
nameLabel.pack()
#Constants:
INIT_STAKE = 100
ITEMS = ["OCELOT", "MACAW", "JAGUAR", "IBIS", "CAIMAN", "BOA", "SCORPION", "TAPIR", "CONDOR", "BAMBOO", "FROG"]
first = None
second = None
third = None
stake = INIT_STAKE
def play():
global stake, first, second, third
playQuestion = askPlayer()
while(stake != 0 and playQuestion == True):
first = spin()
second = spin()
third = spin()
score()
playQuestion = askPlayer()
def askPlayer():
global stake
while(True):
answer = input("You have " + str(stake) + " coins, Roll? ")
print()
if(answer == "y"):
return True
elif(answer == "n"):
print("Game has ended. You won a total of " + str(stake) + " coins")
return False
else:
print("Sorry, I didn't get that")
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):
print(first + '\t' + second + '\t' + third + ' -- You win ' + str(win) + " Coins")
else:
print(first + '\t' + second + '\t' + third + ' -- You lose')
#Background
root.configure(background='seagreen')
play()
答案 0 :(得分:1)
我已修改您的代码以在Tkinter应用程序中使用。但这并不完美,需要在布局上做一些工作,但这应该是一个让您步入正轨的好开始。
我相信您遇到的主要问题是卡在play()
循环中,因此代码从未在tkinter上出现在mainloop()
中。这样可以防止启动GUI。
我修改了play()
函数,改为使用Tkinter中的按钮。如果您有任何问题,请告诉我。
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("700x400")
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()
root.mainloop()