无法实现倒计时器。下面的代码工作,但创建和额外的,不需要的窗口。如果没有TK作为类参数和Tk .__ init行,我会收到关于“after”函数的错误。错误是:
AttributeError:'MakeGUI'对象没有'after'后面的属性
思想?
class MakeGUI(Tk):
def __init__(self,master,width,height,color):
Tk.__init__(self)
self.master = master
self.master.title("Falling Squares Game")
# window not resizable either x or y
self.master.resizable(0, 0)
# make sure window on top
self.master.wm_attributes("-topmost", 1)
#self.master.after(1, lambda: self.master.focus_force())
self.width = width
self.height = height
self.color = color
self.canvas0 = Canvas(self.master,width=750,height=30,bg="light green")
self.canvas0.pack()
self.canvas1 = Canvas(self.master, width = self.width, height = self.height, bg = self.color)
self.canvas1.pack()
self.remaining = 0
# set length of game to n seconds
self.countdown(100)
# count down timer
def countdown(self, remaining = None):
if remaining is not None:
self.remaining = remaining
if self.remaining <= 0:
pass
else:
self.remaining = self.remaining - 1
self.after(1000, self.countdown)
def main():
# create a Tk window
win1 = Tk()
win1x = 750
win1y = 750
# create the GUI
c1 = MakeGUI(win1,win1x,win1y,"light blue")
答案 0 :(得分:0)
代码不会产生你说的错误。
至于两个窗口,它会创建两个窗口,因为你告诉它创建两个窗口。第一个是这行代码:
void kstrextend(kstring *strp, size_t nbytes)
{
char *data1;
int len=strp->length;
if(len < nbytes)
{
//allocate a new array with larger size
data1 = realloc(strp->data, nbytes);
if(data1 == NULL)
{
abort();
}
strp->data = data1;
strp->length = nbytes;
//remaining space of new array is filled with '\0'
for (int i = len; i < nbytes; i++)
{
strp->data[i] = '\0';
}
}
}
第二个是这行代码,因为win1 = Tk()
继承自MakeGUI
:
Tk