File: wx_main.py

package info (click to toggle)
python-pypubsub 4.0.3-7
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 928 kB
  • sloc: python: 4,884; makefile: 156; xml: 16; php: 2
file content (71 lines) | stat: -rw-r--r-- 1,841 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
"""
Adapted from wxPython website at http://wiki.wxpython.org/ModelViewController/.

:copyright: Copyright since 2006 by Oliver Schoenborn, all rights reserved.
:license: BSD, see LICENSE.txt for details.
"""

import wx

from pubsub import pub

print('pubsub API version', pub.VERSION_API)

# notification
from pubsub.utils.notification import useNotifyByWriteFile
import sys

useNotifyByWriteFile(sys.stdout)

# the following two modules don't know about each other yet will
# exchange data via pubsub:
from wx_win1 import View
from wx_win2 import ChangerWidget


class Model:
    def __init__(self):
        self.myMoney = 0

    def addMoney(self, value):
        self.myMoney += value
        # now tell anyone who cares that the value has been changed
        pub.sendMessage("money_changed", money=self.myMoney)

    def removeMoney(self, value):
        self.myMoney -= value
        # now tell anyone who cares that the value has been changed
        pub.sendMessage("money_changed", money=self.myMoney)


class Controller:
    def __init__(self):
        self.model = Model()

        # set up the first frame which displays the current Model value
        self.view1 = View()
        self.view1.setMoney(self.model.myMoney)

        # set up the second frame which allows the user to modify the Model's value
        self.view2 = ChangerWidget()

        self.view1.Show()
        self.view2.Show()

        pub.subscribe(self.changeMoney, 'money_changing')

    def changeMoney(self, amount):
        if amount >= 0:
            self.model.addMoney(amount)
        else:
            self.model.removeMoney(-amount)


if __name__ == "__main__":
    app = wx.App()
    c = Controller()
    sys.stdout = sys.__stdout__

    print('---- Starting main event loop ----')
    app.MainLoop()
    print('---- Exited main event loop ----')