私はサッパーゲーム用の地雷原を描く次のプログラムを書きました
# -*- coding: utf-8 -*-
import mainw, sys
from PyQt4 import QtCore, QtGui
class WindowSapper(QtGui.QMainWindow):
buttons=[]
def __init__(self, parent=None):
QtGui.QMainWindow.__init__(self,parent)
self.ui=mainw.Ui_mainwin()
self.ui.setupUi(self)
for i in xrange(10):
l=[]
for j in xrange(10):
b=QtGui.QPushButton()
l.append(b)
self.ui.gridLayout.addWidget(b, i, j, 1, 1)
self.buttons.append(l)
def main():
app=QtGui.QApplication(sys.argv)
window=WindowSapper()
window.show()
sys.exit(app.exec_())
if __name__ == '__main__':
main()
フォームモジュールも適用します
# -*- coding: utf-8 -*-
# Form implementation generated from reading ui file 'mainw.ui'
#
# Created: Tue Nov 27 08:52:39 2012
# by: PyQt4 UI code generator 4.9.4
#
# WARNING! All changes made in this file will be lost!
from PyQt4 import QtCore, QtGui
try:
_fromUtf8 = QtCore.QString.fromUtf8
except AttributeError:
_fromUtf8 = lambda s: s
class Ui_mainwin(object):
def setupUi(self, mainwin):
mainwin.setObjectName(_fromUtf8("mainwin"))
mainwin.resize(546, 530)
self.centralwidget = QtGui.QWidget(mainwin)
self.centralwidget.setObjectName(_fromUtf8("centralwidget"))
self.gridLayoutWidget = QtGui.QWidget(self.centralwidget)
self.gridLayoutWidget.setGeometry(QtCore.QRect(10, 30, 521, 461))
self.gridLayoutWidget.setObjectName(_fromUtf8("gridLayoutWidget"))
self.gridLayout = QtGui.QGridLayout(self.gridLayoutWidget)
self.gridLayout.setMargin(0)
self.gridLayout.setHorizontalSpacing(6)
self.gridLayout.setObjectName(_fromUtf8("gridLayout"))
mainwin.setCentralWidget(self.centralwidget)
self.menubar = QtGui.QMenuBar(mainwin)
self.menubar.setGeometry(QtCore.QRect(0, 0, 546, 21))
self.menubar.setObjectName(_fromUtf8("menubar"))
mainwin.setMenuBar(self.menubar)
self.statusbar = QtGui.QStatusBar(mainwin)
self.statusbar.setObjectName(_fromUtf8("statusbar"))
mainwin.setStatusBar(self.statusbar)
self.retranslateUi(mainwin)
QtCore.QMetaObject.connectSlotsByName(mainwin)
def retranslateUi(self, mainwin):
mainwin.setWindowTitle(QtGui.QApplication.translate("mainwin", "Сапер", None, QtGui.QApplication.UnicodeUTF8))
class mainwin(QtGui.QMainWindow, Ui_mainwin):
def __init__(self, parent=None, f=QtCore.Qt.WindowFlags()):
QtGui.QMainWindow.__init__(self, parent, f)
self.setupUi(self)
しかし、それは私の期待に応えませんでした.ボタンはGridLayoutで完全に満たされていません.その中には空きスペースがあります.つまり、GridLayoutのセルで完全に満たされていません. これらのギャップを取り除くにはどうすればよいですか?