在 Tkinter 中随时间更改 GUI



我正在使用 {ython 中的 Tkinter 构建康威的生命游戏图形。 游戏逻辑和其他一切都很好,我只是停留在代码的最后一部分,即使在阅读库后我也无法弄清楚。

self.after(1000,self.draw(bigThing))

以下是完整的代码:

from Tkinter import *
from button import *
import numpy
import test1
import time
class AppFrame(Frame):

def __init__(self,root, buttonlist):
    Frame.__init__(self, root, relief='sunken', width=600, height=600)
    self.gameisrunning = False
    self.buttonlist = buttonlist
    self.root = root
    self.w = Canvas(self,width = 505, height =505)

    self.boulis = []
    for k in range(1,101):
        for i in range(1, 101):
            button = Buttons(self.w, i * 5, k * 5, i * 5 + 5, k * 5 + 5,k ,i,buttonlist)
            self.boulis.append(button)

    self.whole = list(self.chunks(self.boulis, 100))
    self.grid(column =0, columnspan = 4)
    self.w.grid(column = 0)
    clear = Button(self.root,text = "clear")
    clear.grid(column = 0, row = 1)
    self.run = Button(self.root,text = "run", command = self.runit)
    self.run.grid(column = 3, row = 1)
    self.root.mainloop()
def runit(self):
    if self.gameisrunning == False:
        self.gameisrunning = True
        self.transformer = self.buttonlist
        while self.gameisrunning == True:
            bigThing = test1.theNextBigThing(self.transformer)
            self.transformer = bigThing
            self.root.after(1000,self.draw, bigThing)

    elif self.gameisrunning == True:
        self.gameisrunning = False

def draw (self, board):
    for k in range (0, 100):
        for i in range (0, 100):
            if board[k + 1][i + 1] == 1:
                self.whole[k][i].activate()
            elif board[k + 1][i + 1] == 0:
                self.whole[k][i].deactivate()
def chunks(self,l, n):
    """ Yield successive n-sized chunks from l.
    """
    for i in xrange(0, len(l), n):
        yield l[i:i+n]

我稍微改变了结构。 似乎我想在"runit"方法下绘制的任何内容都不起作用。 我不知道为什么会这样。 "runit"中的所有其他函数都可以正常工作。 只是这不起作用:

self.root.after(1000,self.draw(bigThing))

这是我的按钮类:

##button.py
from Tkinter import *

class Buttons:
def __init__(self,canvas,bx,by,tx,ty, k, i,buttonlist):
    self.buttonlist = buttonlist
    self.canvas = canvas
    self.rec = canvas.create_rectangle((bx,by,tx,ty),fill = "lightgray",
                                       activefill= 'black', outline = 'lightgray')
    self.canvas.tag_bind(self.rec, "<Button-1>", lambda event: self.callback())
    self.active = False
    self.k = k
    self.i = i
    self.xmin = bx
    self.xmax = tx
    self.ymin = by
    self.ymax = ty
    ##print (bx, by, tx, ty)
def activate(self):
    self.canvas.itemconfigure(self.rec, fill = 'black')
def deactivate(self):
    self.canvas.itemconfig(self.rec, fill='lightgray')
def callback(self):
    self.activate()
    self.buttonlist[self.k][self.i] = 1

你得到这个错误,因为你的AppFrame.__init__没有调用Frame超构造函数——因此,AppFrame对象不是一个合适的Tk对象。该方法中的代码似乎与应该调用它的代码相结合,并且其中声明的Frameself.f)实际上应该self

class AppFrame(Frame):
    def __init__(self, root, buttonlist):
        Frame.__init__(self, root, relief='sunken', width=600, height=600)
        self.root = root
        self.gameisrunning = False
        self.w = Canvas(self.f, width = 505, height =505)
        # [as before, but with self instead of self.f]

在特定于应用程序的框架之外构建root对象通常更加模块化(这允许您将应用嵌入根目录以外的其他内容):

def main():
    root = Tk()
    root.geometry = "1000x1000"
    buttonlist = ... # needs to be filled in
    app = AppFrame(root, buttonlist)
    # ...

相关内容

  • 没有找到相关文章

最新更新