python tkinter pack_python Tkinter grid()和pack()

想用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()

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值