PyQt application architecture

I’m trying to give a sound structure to a PyQt application that implements a card game. So far I have the following classes:

  • Ui_Game: this describes the ui of course and is responsible of
    reacting to the events emitted by my CardWidget instances

  • MainController: this is responsible for managing the whole
    application: setup and all the subsequent states of the application
    (like starting a new hand, displaying the notification of state
    changes on the ui or ending the game)

  • GameEngine: this is a set of classes that implement the whole game
    logic

Now, the way I concretely coded this in Python is the following:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>class CardWidget(QtGui.QLabel):
def __init__(self, filename, *args, **kwargs):
QtGui.QLabel.__init__(self, *args, **kwargs)
self.setPixmap(QtGui.QPixmap(':/res/res/' + filename))
def mouseReleaseEvent(self, ev):
self.emit(QtCore.SIGNAL('card_clicked'), self)
class Ui_Game(QtGui.QWidget):
def __init__(self, window, *args, **kwargs):
QtGui.QWidget.__init__(self, *args, **kwargs)
self.setupUi(window)
self.controller = None
def place_card(self, card):
cards_on_table = self.played_cards.count() + 1
print cards_on_table
if cards_on_table <= 2:
self.played_cards.addWidget(card)
if cards_on_table == 2:
self.controller.play_hand()
class MainController(object):
def __init__(self):
self.app = QtGui.QApplication(sys.argv)
self.window = QtGui.QMainWindow()
self.ui = Ui_Game(self.window)
self.ui.controller = self
self.game_setup()
</code>
<code>class CardWidget(QtGui.QLabel): def __init__(self, filename, *args, **kwargs): QtGui.QLabel.__init__(self, *args, **kwargs) self.setPixmap(QtGui.QPixmap(':/res/res/' + filename)) def mouseReleaseEvent(self, ev): self.emit(QtCore.SIGNAL('card_clicked'), self) class Ui_Game(QtGui.QWidget): def __init__(self, window, *args, **kwargs): QtGui.QWidget.__init__(self, *args, **kwargs) self.setupUi(window) self.controller = None def place_card(self, card): cards_on_table = self.played_cards.count() + 1 print cards_on_table if cards_on_table <= 2: self.played_cards.addWidget(card) if cards_on_table == 2: self.controller.play_hand() class MainController(object): def __init__(self): self.app = QtGui.QApplication(sys.argv) self.window = QtGui.QMainWindow() self.ui = Ui_Game(self.window) self.ui.controller = self self.game_setup() </code>
class CardWidget(QtGui.QLabel):
    def __init__(self, filename, *args, **kwargs):
        QtGui.QLabel.__init__(self, *args, **kwargs)
        self.setPixmap(QtGui.QPixmap(':/res/res/' + filename))

    def mouseReleaseEvent(self, ev):
        self.emit(QtCore.SIGNAL('card_clicked'), self)

class Ui_Game(QtGui.QWidget):
    def __init__(self, window, *args, **kwargs):
        QtGui.QWidget.__init__(self, *args, **kwargs)
        self.setupUi(window)
        self.controller = None

    def place_card(self, card):
        cards_on_table = self.played_cards.count() + 1
        print cards_on_table

        if cards_on_table <= 2:
            self.played_cards.addWidget(card)
            if cards_on_table == 2:
                self.controller.play_hand()

class MainController(object):
    def __init__(self):
        self.app = QtGui.QApplication(sys.argv)
        self.window = QtGui.QMainWindow()
        self.ui = Ui_Game(self.window)
        self.ui.controller = self
        self.game_setup()

Is there a better way other than injecting the controller into the Ui_Game class in the Ui_Game.controller? Or am I totally off-road?

I would say you should do the contrary, a good design would be for your application to be able to run whatever its context is. That’s why for every application I code, I create a CLI tool that implements the logic, and then I implement a GUI. Where am I heading to ?

You’re creating a MainController object that holds the world, and then modifies the Ui_Game object to give back a reference to itself. That’s not elegant, and given your context that’s not the best way.

If you really want to go this way you should instead do the following:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>class Ui_Game(QtGui.QWidget):
def __init__(self, controller, *args, **kwargs):
QtGui.QWidget.__init__(self, *args, **kwargs)
self.setupUi(controller.window)
self.controller = controller
[...]
Ui_Game(self)
</code>
<code>class Ui_Game(QtGui.QWidget): def __init__(self, controller, *args, **kwargs): QtGui.QWidget.__init__(self, *args, **kwargs) self.setupUi(controller.window) self.controller = controller [...] Ui_Game(self) </code>
class Ui_Game(QtGui.QWidget):
    def __init__(self, controller, *args, **kwargs):
        QtGui.QWidget.__init__(self, *args, **kwargs)
        self.setupUi(controller.window)
        self.controller = controller

[...]

Ui_Game(self)

That’s for the elegancy.

But for your context, I’d advise you to totally separate the game logic from the UI logic, and thus have some start or main function that initializes all your objects, and bind them together. Also, you create a widget that seems to populate itself inside the window, whereas the widget shall not know where it will be used. So here something close to what I’d do:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>class GameGUI:
def __init__(self, game):
# so you can give all your UI elements a game to play with
self.game = game
# start and populate the Ui_Game stuff
self.app = QtGui.QApplication(sys.argv)
def main_loop(self):
# wait for the UI to cease
return self.app._exec()
def start():
# here you can argparse your CLI arguments, so you can choose
# your interface (readline, ncurses, Qt, web, whatever...?)
# and setup your application (logfile, port to bind to, look
# of the GUI...)
game = MyGame()
game.setup()
return GameGUI(game).main_loop()
import sys
if __name__ == "__main__":
sys.exit(start())
</code>
<code>class GameGUI: def __init__(self, game): # so you can give all your UI elements a game to play with self.game = game # start and populate the Ui_Game stuff self.app = QtGui.QApplication(sys.argv) def main_loop(self): # wait for the UI to cease return self.app._exec() def start(): # here you can argparse your CLI arguments, so you can choose # your interface (readline, ncurses, Qt, web, whatever...?) # and setup your application (logfile, port to bind to, look # of the GUI...) game = MyGame() game.setup() return GameGUI(game).main_loop() import sys if __name__ == "__main__": sys.exit(start()) </code>
class GameGUI:
    def __init__(self, game):
        # so you can give all your UI elements a game to play with
        self.game = game 
        # start and populate the Ui_Game stuff
        self.app = QtGui.QApplication(sys.argv)

    def main_loop(self):
        # wait for the UI to cease
        return self.app._exec()

def start():
    # here you can argparse your CLI arguments, so you can choose
    # your interface (readline, ncurses, Qt, web, whatever...?)
    # and setup your application (logfile, port to bind to, look 
    # of the GUI...)
    game = MyGame()
    game.setup()
    return GameGUI(game).main_loop()

import sys
if __name__ == "__main__":
    sys.exit(start())

So, now the flow is more limpid:

  • you start in the start() function, which will prepare the environment for your application to run correctly
    • check arguments if any,
    • initializes the Game,
    • give it to the GUI,
    • build the GUI (through GameGUI that will create the windows and the widgets)
    • run the GUI (through main_loop())

Always remember that OOP is all about decoupling.

HTH

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật