【问题标题】:Logic within python and Tkinter: how to make a window run endlesslypython 和 Tkinter 中的逻辑:如何使窗口无休止地运行
【发布时间】: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

标签: python tkinter logic


【解决方案1】:

需要调查的一些事情:你有while error == False,但在循环中对error 的唯一引用是将False 分配给它。在循环中,您分配self.user = False,但循环中的几乎所有内容都依赖于进入if self.user == True。您将self.userinput.get() 分配给local 变量value,而不是分配给实例变量。您应该更多地使用in 运算符以避免进行数十次单独的比较。您只是在if self.user == True 中的大if 语句中检查value[1]。无需检查值是否等于TrueFalse,您可以只引用该值,例如if self.user:,因为表达True == True 的另一种方式就是True(除非该变量具有不确定的类型,并且您确实需要检查它是否是True,或者说是非空字符串)。您正在检查 Entry 框中的内容,然后才实际准备好 - 您应该将 request_ships() 放入某个 Button 的命令中,或者至少等待 after() 的内容。您正在检查self.board[i][0] 是否等于value[0]value[1],只有当用户输入像AA 这样的无效内容时才会如此。如果我们修复它以同时检查self.board[i][1],它仍然无法工作,因为Entry 小部件中的所有内容都是一个字符串,必须将其转换为int。您不能将字符串连接到 list - 使用 append()。从单个函数依次检查垂直/水平、位置等会遇到问题,因为您永远不会等待用户输入不同的值。

以下是request_ship() 的示例,其中实施了一些更改,以便您了解该去哪里:

def request_ship(self,allegiance,name,size):
    print allegiance,name,size
    ship = [name,size]
    error = True
    if allegiance == "player":
        print "here"
        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:    
            if self.value not in ("vertical", "horizontal", "h", "v"):
                self.errorlabel.configure(text = "Error! Incorrect Format!")
            else:
                self.user = False
                if self.value == "h":
                    ship.append(self.value)
                    self.infoLabel2.configure(text = "Please enter the leftmost coordinate of where you want to place your ship:")
                    if self.user:
                        try:
                            v1 = int(self.value[1:])
                        except ValueError:
                            pass
                        if 1 < len(self.value) <= 3 and (self.value[0] in "ABCDEFGHIJ") and (v1 in range(1,11)):
                            for i in range(len(self.board)):
                                if self.board[i][0] == self.value[0] and self.board[i][1] == v1:
                                    num = 0
                                    for i in size:
                                        coord+= i + num
                                        num += 1
            self.user = False

请注意,上述代码既不是完整的,也不是使您的程序正常工作的直接替换。您的代码需要进行一些认真的重组来处理作为 GUI 程序与命令行程序的区别。

【讨论】:

  • 我不能投票,所以那不是我,我也不能给你投票。对不起:l
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-07-18
  • 1970-01-01
  • 2011-11-08
  • 2017-01-08
  • 1970-01-01
相关资源
最近更新 更多