想用Tkinter做一个桌面版的#字棋游戏,但用Tkinter的grid方法时发现程序会陷入死循环,一直没有结果。但如果不用类的方式直接写则代码皆可实现,初学python,找不到原因!求助各位大神,代码不足的地方请多多指教 ^_^
# -*- coding:utf-8 -*-
#---Desktop Edition---
import Tkinter as tk
class Tictactoe:
def __init__(self,master):
self.master = master
master.title('hello,world')
master.geometry('400x400')
frame = tk.Frame(master)
frame.pack()
#-------------create the menu bar------------
menubar = tk.Menu(master)
#创建下拉菜单File,然后将其加入到顶级的菜单栏中
filemenu = tk.Menu(menubar,tearoff=0)
filemenu.add_command(label="Restart", command=self.hello)
filemenu.add_command(label="Save", command=self.hello)
filemenu.add_separator()
filemenu.add_command(label="Exit", command=self.master.quit)##???
menubar.add_cascade(label="File", menu=filemenu)
#创建下拉菜单Help
helpmenu = tk.Menu(menubar, tearoff=0)
helpmenu.add_command(label="About", command=self.about)
menubar.add_cascade(label="Help", menu=helpmenu)
#显示菜单
master.config(menu=menubar)
btn_00 = tk.Button(master,text='x',command=self.event,width=15,height=5).grid(row=0,column=1)
def add_btn(self):
btn_00 = tk.Button(self.master,text='x',command=self.event,width=15,height=5).grid(row=0,column=1)
btn_01 = tk.Button(self.master,command=self.event,width=15,height=5)
btn_01.grid(row=0,column=1)
btn_02 = tk.Button(self.master,command=self.event,width=15,height=5)
btn_02.grid(row=0,column=2)
#----------------------------------------
btn_10 = tk.Button(self.master,command=self.event,width=15,height=15)
btn_10.grid(row=1,column=0)
btn_11 = tk.Button(self.master,command=self.event,width=15,height=15)
btn_11.grid(row=1,column=1)
btn_12 = tk.Button(self.master,command=self.event,width=15,height=15)
btn_12.grid(row=1,column=2)
#----------------------------------------
btn_20 = tk.Button(self.master,command=self.event,width=15,height=15)
btn_20.grid(row=2,column=0)
btn_21 = tk.Button(self.master,command=self.event,width=15,height=15)
btn_21.grid(row=2,column=1)
btn_22 = tk.Button(self.master,command=self.event,width=15,height=15)
btn_22.grid(row=2,column=2)
def event(self):
print 'this is a event'
def hello(self):
print 'hello,world'
def about(self):
w = tk.Label(self.master,text='this is a programe about tictactoe!\n we really need your opinion\nplease email us:wenyuhua90 at gmail.com')
w.pack(side='top')
def run(self):
self.master.resizable(False, False)
#self.add_btn()
print 'add_btn'
self.master.mainloop()
print 'run'
################################################################
if __name__=="__main__":
top = tk.Tk()
tic = Tictactoe(top)
tic.run()