当我使用tkinter按下按钮时,我的程序崩溃了

时间:2018-01-21 16:59:50

标签: python class button tkinter

当我尝试运行程序时,按下continue_button_two按钮(append_to_board功能)时一切正常;但是当我按下continue_button_two buttonappend_to_board function之后我按下了ship_buttonappend_to_board函数)时程序崩溃了:

import random
from tkinter import *
class Application():
    def __init__(self, master):
        self.frame=Frame(master)
        self.frame.pack()

        self.row_label=Label(self.frame, text='Rows')
        self.row_label.pack()
        self.row=Entry(self.frame)
        self.row.pack()

        self.column_label=Label(self.frame, text='Columns')
        self.column_label.pack()
        self.column=Entry(self.frame, text='How many columns')
        self.column.pack()

        self.continue_button_one=Button(self.frame, text='Continue', command=self.append_to_board)
        self.continue_button_one.pack()
        self.ships=0
    def append_to_board(self):
        print(self)
        self.row_var=int(self.row.get())
        self.column_var=int(self.column.get())
        self.row.pack_forget()
        self.column.pack_forget()
        self.continue_button_one.pack_forget()
        self.row_label.pack_forget()
        self.column_label.pack_forget()

        self.ships_list=[]
        self.ship_button=Button(self.frame, text='Create enemy boat!', command=self.create_ship)
        self.ship_button.pack()
        self.continue_button_two=Button(self.frame, text='Continue', command=self.play)
        self.continue_button_two.pack()

    def play(self):
        self.ship_button.pack_forget()
        self.continue_button_two.pack_forget()
        self.board=[['0' for i in range(int(self.row.get()))] for i in range(int(self.column.get()))]
        for i in self.board:
            self.board_str=''.join(i)
            self.board_label=Label(self.frame, text=self.board_str)
            self.board_label.pack()
        while self.ships!=0:
            self.x_coord=Entry(self.frame)
            self.y_coord=Entry(self.frame)
            for i in self.ships_list:
                if [self.x_coord, self.y_coord] == i:
                    print('\n'*1000+'Hit')
                    self.ships-=1
    def create_ship(self):
        self.x=random.randint(0,self.row_var)
        self.y=random.randint(0,self.column_var)
        self.ships_list.append([int(self.x),int(self.y)])
        for i in range(len(self.ships_list)):
            self.ships+=1
        self.ship_button.pack_forget()
        self.continue_button_two.pack_forget()
        self.append_to_board()

root=Tk()
a=Application(root)
root.mainloop()

1 个答案:

答案 0 :(得分:0)

只需将while更改为if函数中的play语句:

def play(self):
        self.ship_button.pack_forget()
        self.continue_button_two.pack_forget()
        self.board=[['0' for i in range(int(self.row.get()))] for i in range(int(self.column.get()))]
        for i in self.board:
            self.board_str=''.join(i)
            self.board_label=Label(self.frame, text=self.board_str)
            self.board_label.pack()
        ##the arrow indicates the conversion and is not actuall syntax
        while --> if self.ships!=0:
            self.x_coord=Entry(self.frame)
            self.y_coord=Entry(self.frame)
            for i in self.ships_list:
                if [self.x_coord, self.y_coord] == i:
                    print('\n'*1000+'Hit')
                    self.ships-=1