考虑此代码:
import pygtk
import gtk
class TheBoard:
def delete_event(self, widget, event, data=None):
return False # confirms delete events
def destroy(self, widget, data=None):
gtk.main_quit()
def __init__(self, grid=4):
self.window = gtk.Window(gtk.WINDOW_TOPLEVEL)
self.window.set_title("Game Board")
self.window.connect("delete_event", self.delete_event)
self.window.connect("destroy", self.destroy)
self.window.set_border_width(10)
self.table = gtk.Table(grid, grid, True)
self.spots = [gtk.Label('unk')] * (grid*grid)
for index, spot in enumerate(self.spots):
spot.set_text("%d" % index)
x = index / grid
y = index % grid
spot.show()
self.table.attach(spot, x, x+1, y, y+1, xoptions=gtk.FILL, yoptions=gtk.FILL, xpadding=0, ypadding=0)
print "(%d, %d)" % (x, y)
print "Attaching %s as (%d, %d, %d, %d)" % (spot.get_text(), x, x+1, y, y+1)
self.window.add(self.table)
self.table.show()
self.window.show()
def main(self):
gtk.main()
if __name__ == '__main__':
gb = TheBoard()
gb.main()
这个输出:
main.py:30: GtkWarning: IA__gtk_table_attach: assertion 'child->parent == NULL' failed
self.table.attach(spot, x, x+1, y, y+1, xoptions=gtk.FILL, yoptions=gtk.FILL, xpadding=0, ypadding=0)
(0, 0)
Attaching 0 as (0, 1, 0, 1)
(0, 1)
Attaching 1 as (0, 1, 1, 2)
(0, 2)
Attaching 2 as (0, 1, 2, 3)
(0, 3)
Attaching 3 as (0, 1, 3, 4)
(1, 0)
Attaching 4 as (1, 2, 0, 1)
(1, 1)
Attaching 5 as (1, 2, 1, 2)
(1, 2)
Attaching 6 as (1, 2, 2, 3)
(1, 3)
Attaching 7 as (1, 2, 3, 4)
(2, 0)
Attaching 8 as (2, 3, 0, 1)
(2, 1)
Attaching 9 as (2, 3, 1, 2)
(2, 2)
Attaching 10 as (2, 3, 2, 3)
(2, 3)
Attaching 11 as (2, 3, 3, 4)
(3, 0)
Attaching 12 as (3, 4, 0, 1)
(3, 1)
Attaching 13 as (3, 4, 1, 2)
(3, 2)
Attaching 14 as (3, 4, 2, 3)
(3, 3)
Attaching 15 as (3, 4, 3, 4)
渲染板:The Game Board
创建gtk.Label
或gtk.Table
对象时,我做错了什么?我想我搞乱了gtk.Table.attach()
功能。也许我现在有点密集,但是我在查找函数调用的正确参数时遇到了一些困难。我将我的代码建模为this tutorial,但我的代码不起作用。它运行,但显示一个警告,看起来像最后gtk.Label
覆盖了先验。
我看过很多文章,但所有gtk.Table
教程都显示代码明确列出了附加的每个项目,而不是动态的东西,这正是我想要做的。我想在一个循环中创建项目,因此该板可以是任何尺寸(使其成为正方形以使其变得容易)。
有什么想法?我在哪里弄错了?