【发布时间】:2018-06-30 08:41:38
【问题描述】:
当我尝试运行程序时,当我按下continue_button_two 按钮时一切正常(append_to_board 功能);但是当我按下continue_button_two button (append_to_board function) 之后 我按下了ship_button (append_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()
【问题讨论】:
-
你能告诉我们
pack_forget函数吗? -
@kingJulian 这是来自 tkinter 的方法
-
您不应在标题中添加 SOLVED,发布答案并将其标记为正确。
-
请在您的问题中包含错误。
标签: python class button tkinter