python获得游戏按钮_Python扫雷游戏gtk3在cli后获取按钮值

我用Gtk3用python2.7编写简单的扫雷游戏。我在单击后使用set_sensitive(False)显示标签值有问题。在

第一种情况下,我在点击后使用button.hide(),这样很好,标签值就会显示出来。在

bhQFx.jpg

船上有地雷吗

但我不想用hide()。我想用set_sensitive()属性在单击后阻止按钮。我尝试在我的discover函数return self.button.set_sensitive(False)中执行此操作,但在那之后,我没有得到任何按钮值。我点击了所有的板和所有的按钮是禁用的。为什么我没有得到任何价值的标签?在

板使用后set_sensitive(False)

QzkbM.jpg

我的代码:import gi

from random import randrange

gi.require_version('Gtk', '3.0')

from gi.repository import Gtk, Gdk

class SaperButton(Gtk.Button):

def __init__(self):

Gtk.Button.__init__(self)

self.set_size_request(50, 50)

class Cell:

def __init__(self):

self.mine = False

self.neighbormines = 0

self.button = SaperButton()

def place_mine(self):

self.mine = True

def is_mine(self):

return self.mine

def discover(self):

print 'discover'

# with hide showing values of label

# with return self.button.set_sensitive(False) don't show

return self.button.hide()

def is_discovered(self):

return not self.button.get_visible()

def set_nighbromines(self, number):

self.neighbormines = number

def get_nighbromines(self):

return self.neighbormines

def get_button(self):

return self.button

class SaperGrid(Gtk.Grid):

def __init__(self, rows, cols, ratio):

self.rows = rows

self.cols = cols

self.cells = []

self.ratio = ratio

Gtk.Grid.__init__(self)

for row in range(rows):

for col in range(cols):

cell = Cell()

self.cells.append(cell)

self.attach(cell.get_button(), row, col, 1, 1)

self.place_mines()

def get_cells(self):

return self.cells

def get_row_col_button(self, index):

return (index / self.cols, index % self.cols)

def place_mines(self):

mines = 0

while mines < (self.rows * self.cols * self.ratio):

row = randrange(0, self.rows)

col = randrange(0, self.cols)

i = self.get_index(row, col)

if not self.cells[i].is_mine():

mines += 1

self.cells[i].place_mine()

button = Gtk.Button()

label = Gtk.Label("M")

button.add(label)

self.attach(button, row, col, 1, 1)

for i, val in enumerate(self.cells):

print self.cells[i]

def get_index(self, row, col):

return (row * self.cols) + col

def discover_cell(self, row, col):

index = self.get_index(row, col)

print 'index', index

self.cells[index].discover()

def discover_all_cells(self):

for cell in self.cells:

cell.discover()

class Saper:

def __init__(self, rows, cols):

self.window = Gtk.Window()

self.rows = rows

self.cols = cols

self.vbox = Gtk.VBox()

self.window.add(self.vbox)

self.create_grid(rows, cols)

def create_grid(self, rows, cols):

self.grid = SaperGrid(rows, cols, 0.10)

for i, cell in enumerate(self.grid.get_cells()):

(row, col) = self.grid.get_row_col_button(i)

print 'Button connect in col {} row {}'.format(col, row)

cell.get_button().connect('clicked', self.clicked_handler, row, col)

self.grid.set_column_homogeneous(True)

self.grid.set_row_homogeneous(True)

self.vbox.pack_start(self.grid, expand=True, fill=True, padding=0)

def clicked_handler(self, button, row, col):

cell_index = self.grid.get_index(row, col)

self.grid.discover_cell(row, col)

@staticmethod

def exit(self, widget, data=None):

Gtk.main_quit()

win = Saper(5, 5)

win.window.show_all()

Gtk.main()

怎么了?在

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值