【发布时间】:2015-07-28 13:44:57
【问题描述】:
我目前正在尝试使用 Tkinter 构建战舰游戏。然而,我陷入了僵局,因为我无法让程序正确“等待”答案并显示 Tkinter 窗口。 我的想法是,每当按下提交按钮时,都会将一个值设置为 true。只要那个值是假的,那么程序就会循环一个while循环。当该值被触发时,将给出下一组操作,并将该值设置回 false,并再次等待它为 true。代码如下:
from Tkinter import *
import time
class Application(Frame):
def __init__(self,master):
self.user = False
Frame.__init__(self,master)
self.launch()
self.master = master
self.shiplist = []
self.board = [
["A",1],["B",1],["C",1],["D",1],["E",1],["F",1],["G",1],["H",1],["I",1],["J",1],
["A",2],["B",2],["C",2],["D",2],["E",2],["F",2],["G",2],["H",2],["I",2],["J",2],
["A",3],["B",3],["C",3],["D",3],["E",3],["F",3],["G",3],["H",3],["I",3],["J",3],
["A",4],["B",4],["C",4],["D",4],["E",4],["F",4],["G",4],["H",4],["I",4],["J",4],
["A",5],["B",5],["C",5],["D",5],["E",5],["F",5],["G",5],["H",5],["I",5],["J",5],
["A",6],["B",6],["C",6],["D",6],["E",6],["F",6],["G",6],["H",6],["I",6],["J",6],
["A",7],["B",7],["C",7],["D",7],["E",7],["F",7],["G",7],["H",7],["I",7],["J",7],
["A",8],["B",8],["C",8],["D",8],["E",8],["F",8],["G",8],["H",8],["I",8],["J",8],
["A",9],["B",9],["C",9],["D",9],["E",9],["F",9],["G",9],["H",9],["I",9],["J",9],
["A",10],["B",10],["C",10],["D",10],["E",10],["F",10],["G",10],["H",10],["I",10],["J",10],
]
self.letters = [["A",1],["B",2],["C",3],["D",4],["E",5],["F",6],["G",7],["H",8],["I",9],["J",10]]
def launch(self):
self.pack(fill=BOTH, expand=1)
Label(self, text="Your Board").grid(sticky=W,row = 0, column = 0)
Label(self, text="Enemy Board").grid(sticky=W, row = 0,column = 2)
canvas1 = Canvas(self)
canvas1.create_rectangle(2,2,250,250,outline= "black",fill = "white")
canvas1.grid(row=1, column=0, columnspan=2, rowspan=2)
canvas2 = Canvas(self)
canvas2.create_rectangle(2,2,250,250,outline= "black",fill = "white")
canvas2.grid(row=1, column=2, columnspan=2, rowspan=2)
self.actionLabel = Label(self,text = "Currently Placing:")
self.actionLabel.grid(row = 3,column= 0,sticky = W)
self.infoLabel1 = Label(self,text ="")
self.infoLabel1.grid(row = 4,column = 0)
self.infoLabel2 = Label(self,text = "")
self.infoLabel2.grid(row=5,column=0)
self.userinput = Entry(self,text = "Write here")
self.userinput.grid(row=6,column=0)
Button(self,text = "Submit",command = self.action).grid(row = 6,column = 1,sticky = W)
self.errorlabel = Label(self,text = "")
self.errorlabel.grid(row=7,column=0)
self.request_ships()
def action(self):
value = self.userinput.get()
self.user = True
print "action successful"
def request_ship(self,allegiance,name,size):
print allegiance,name,size
ship = [name,size]
error = False
if allegiance == "player":
print "here"
while error == False:
self.user = False
text1 = name + "Size: "+ str(size) + " cells."
self.infoLabel1.configure(text = text1)
self.infoLabel2.configure(text = "Would you like for your ship to be vertical or horizontal? v/h:")
if self.user == True:
if value != "vertical" and value != "horizontal" and value != "h" and value != "v":
self.errorlabel.configure(text = "Error! Incorrect Format!")
error = False
else:
self.user = False
if value == "h":
ship += value
self.infoLabel2.configure(text = "Please enter the leftmost coordinate of where you want to place your ship:")
if self.user== True:
if len(value) > 1 and len(value) <= 3 and (value[0] == "A"
or value[0] == "B" or value[0] == "C" or value[0] == "D"
or value[0] == "E" or value[0] == "F" or value[0] == "G"
or value[0] == "H" or value[0] == "I" or value[0] == "J") and (value[1] == "1"
or value[1] == "2" or value[1] == "3" or value[1] == "4"
or value[1] == "5" or value[1] == "6" or value[1] == "7"
or value[1] == "8" or value[1] == "9" or value[1] == "10"):
for i in range(len(self.board)):
if self.board[i][0] == value[0] and self.board[i][0] == value[1:]:
num = 0
for i in size:
coord+= i + num
num += 1
print ship
def request_ships(self):
print "request ships works"
self.request_ship("player","Aircraft Carrier",5)
def grid(self,gridinfo,shipinfo):
coord = 0
cellnum = 0
for number in range(100):
cell = Canvas(self)
for ship in shipinfo:
## if #####
print "tt"
cell.create_rectangle(coord,coord,25,25,outline="black",fill=color)
def main():
root = Tk()
app = Application(root)
app.geometry = ("1000x1000")
root.title("Battleship")
root.mainloop()
main()
然而,问题是,当 request_ships 函数加载时,该窗口永远不会出现,因为它在一个 while 循环中无休止地进行。有关如何解决此问题的任何提示?
【问题讨论】:
-
一些元提示:1)搜索tkinter输入背景排序的关键字。答案中已经描述了很多有用的方法。 2)stackoverflow.com/help/mcve