Pythonic gui format?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Gregory Petrosyan

    #1

    Pythonic gui format?

    Buenos dias, amigos!
    I have to write _simple_ gui library, for embedding into game. My
    first attempt was to use XML: isn't it cute to describe ui in such a
    way:

    <window>
    <title>Hello World!</title>
    <image text="nice picture here" pos=... src=... />
    <text opts=...>
    (some text here)
    </text>
    <list>
    <item>first element</item>
    <item>second one...</item>
    </list>
    <button action=... text="Click Me!" />
    </window>

    (or something similar)

    But after reading "Python Is Not Java"
    http://dirtsimple.org/2004/12/python-is-not-java.html (BTW I'm not a
    Java programmer) I realised that it is actually not cute at all :) (am
    I wrong here?)
    I am currently seeking for pythonic alternative for XML. One possible
    way is to create a class for every (type of?) window, like

    class SomeDlg(Window) :
    def __init__(self, dict?):
    self.setdict(di ct)
    self.add_text(" $friend would like to speak to you. $question")
    self.add_button (action=self.on click,
    text="Yeah, I'd like to $action !")
    ...

    def onclick(self):
    # process data here, send some messages etc.


    Isn't it ugly a bit? Another variant is

    dlg = Window(dict, [opts])
    dlg.text("text here", [opts])
    dlg.image(src, pos, text [etc])
    .....

    or possibly

    dlg = Window(...)
    dlg.text = Text(...)
    dlg.button = Button(...)
    ....
    dlg.display(sur face)


    These styles of course can be combined... But IMHO looks not very nice
    :( Maybe I shall not rely on introspection? Any suggestions, comments,
    thoughts and links are welcome.
    Thanks.

  • Ivan Voras

    #2
    Re: Pythonic gui format?

    Gregory Petrosyan wrote:[color=blue]
    > I have to write _simple_ gui library, for embedding into game. My
    > first attempt was to use XML[/color]

    ....
    [color=blue]
    > But after reading "Python Is Not Java"
    > http://dirtsimple.org/2004/12/python-is-not-java.html (BTW I'm not a
    > Java programmer) I realised that it is actually not cute at all :) (am
    > I wrong here?)[/color]

    Don't do that :)

    Don't listen to recommendations for or against a technology based only
    on vague "this is not how we do things around here" reasons. If XML
    suits your need, than use it. It seems it will work fine in situations
    where you need to edit/extend the GUI by external means. If you do it in
    code, you risk that you'll need to change the code every time you want
    to reposition a GUI element. It doesn't matter much if you invent your
    own file format because eventually you'll have to extend it, or write
    support for it, so you could as well use XML from the start.

    XML parsing can be a little tedious, but there are many tools that make
    it easier (I've even written one: http://ivoras.sharanet.org/xmldict.py.gz).

    Comment

    • Gregory Petrosyan

      #3
      Re: Pythonic gui format?

      Thanks for your reply.
      But isn't python code more flexible than XML? I think it's not harder
      to edit py file than xml file. Also python can give me more speed than
      xml, and possibly python code can be integrated better with rest of app
      (because of absence of extra layer called "XML"). Has anybody have had
      (how many grammar errorrs can you find here? :) some experience in
      writing gui systems/formats "from scratch"?

      IMO the strength of XML here is that it provides one, almost obvious,
      format for gui description. And with Python, I can imagine hundreds of
      them :) (but maybe there are real gems, and after finding one I'll be
      very happy?)

      Comment

      • Georg Brandl

        #4
        Re: Pythonic gui format?

        Gregory Petrosyan wrote:[color=blue]
        > Buenos dias, amigos!
        > I have to write _simple_ gui library, for embedding into game. My
        > first attempt was to use XML: isn't it cute to describe ui in such a
        > way:
        >
        > <window>
        > <title>Hello World!</title>
        > <image text="nice picture here" pos=... src=... />
        > <text opts=...>
        > (some text here)
        > </text>
        > <list>
        > <item>first element</item>
        > <item>second one...</item>
        > </list>
        > <button action=... text="Click Me!" />
        > </window>[/color]

        One could imagine to model this with Python, like this:

        class FooDlg(Window):
        title = "Hello World!"
        size = (400, 300)

        class WelcomeImg(Imag e):
        filename = "..."
        pos = (0, 0)

        class Box(Frame):
        text = "My Controls"

        class NameLbl(Label):
        text = "Name:"

        class NameField(TextB ox):
        length = 100
        pos = ...
        def onClick(self):
        MessageBox(self .text)

        class TestList(ListBo x):
        items = ['First', 'Second']

        _Slight_ misuse of "class" though...

        Georg

        Comment

        • Gregory Petrosyan

          #5
          Re: Pythonic gui format?

          Nice, thanks!
          BTW, maybe decorators could do good job here (something like @expose in
          TG?)

          Comment

          • James Stroud

            #6
            Re: Pythonic gui format?

            Ivan Voras wrote:[color=blue]
            > Gregory Petrosyan wrote:[color=green]
            >> But after reading "Python Is Not Java"
            >> http://dirtsimple.org/2004/12/python-is-not-java.html (BTW I'm not a
            >> Java programmer) I realised that it is actually not cute at all :) (am
            >> I wrong here?)[/color]
            >
            > Don't do that :)
            >
            > Don't listen to recommendations for or against a technology based only
            > on vague "this is not how we do things around here" reasons.[/color]

            The reasons given in the blog were fairly precise. I think "only on
            vague[...]" is misleading here. The idea of the article was to educate a
            Java user how to change his or her frame of reference to better use Python.

            The author was not being territorial as you are implying.

            Comment

            • Paul Boddie

              #7
              Re: Pythonic gui format?

              Gregory Petrosyan wrote:[color=blue]
              > Thanks for your reply.
              > But isn't python code more flexible than XML? I think it's not harder
              > to edit py file than xml file.[/color]

              It's all about integration with tools, and while there are various
              situations where avoiding tools is often better than extensive use of
              tools (Python vs. Java-plus-Eclipse being an example that springs to my
              mind, at least), there are some very good tools for certain kinds of
              GUI design; it's better in such situations to make use of those tools
              rather than eschew them in favour of something "Pythonic" but, in
              various respects, less usable.

              Once upon a time, it may have been the case that GUI design tools
              (typically for forms-based applications) only produced program code - I
              think SpecTcl, for example, was admired for its capabilities but
              criticised for its Tcl-centric nature, ultimately forcing many to look
              elsewhere despite variants such as SpecPython - but most design tools
              now seem to produce some kind of XML description of widgets and
              dialogues. How usable to other languages would Glade or Qt Designer be
              if they respectively produced just C and C++ code? (Actually, unlike
              Tcl, consuming C or C++ produced from the same tools would be an
              annoyance in itself, I'd imagine.)

              Paul

              Comment

              • Gregory Petrosyan

                #8
                Re: Pythonic gui format?

                I need format, specialised for manual editing.
                (And in this case a) I don't want/need to develop/find GUI forms
                editors etc; b) maybe python is better for manual work? XML is pretty
                nice for machines, but what about human mind: what is closer to it?)

                Anyway, thanks for your help & attention.

                Comment

                • Bruno Desthuilliers

                  #9
                  Re: Pythonic gui format?

                  Gregory Petrosyan a écrit :[color=blue]
                  > Buenos dias, amigos!
                  > I have to write _simple_ gui library, for embedding into game. My
                  > first attempt was to use XML: isn't it cute to describe ui in such a
                  > way:
                  >
                  > <window>
                  > <title>Hello World!</title>
                  > <image text="nice picture here" pos=... src=... />
                  > <text opts=...>
                  > (some text here)
                  > </text>
                  > <list>
                  > <item>first element</item>
                  > <item>second one...</item>
                  > </list>
                  > <button action=... text="Click Me!" />
                  > </window>
                  >
                  > (or something similar)
                  >
                  > But after reading "Python Is Not Java"
                  > http://dirtsimple.org/2004/12/python-is-not-java.html (BTW I'm not a
                  > Java programmer) I realised that it is actually not cute at all :) (am
                  > I wrong here?)[/color]

                  Well, XML is a bit on the verbose side (but less than Java, which is why
                  Javaers love XML), and we usually have better ways to express things in
                  Python. But this doesn't mean that you shouldn't use it when appropriate.
                  [color=blue]
                  > I am currently seeking for pythonic alternative for XML.[/color]

                  A pretty obvious one is dicts and lists. What about (Q&D):

                  window = {
                  'title' : 'Hello World!'
                  'image' : {'text' :"nice picture here",
                  'pos' : ...,
                  'src' : .. },
                  'text_opts' : """
                  (some text here)
                  """,
                  'items' : [
                  'first-element',
                  'second-one',
                  ...
                  ]
                  'button' : {
                  'action': ...
                  'text' :"Click Me!"
                  },
                  }


                  well... It sure needs more work, but you get the idea...
                  [color=blue]
                  > One possible
                  > way is to create a class for every (type of?) window, like
                  >[/color]
                  (snip ugly code)[color=blue]
                  >
                  > Isn't it ugly a bit?[/color]

                  I'd even say 'ugly 16-bits' !-)
                  [color=blue]
                  > These styles of course can be combined... But IMHO looks not very nice
                  > :( Maybe I shall not rely on introspection?[/color]

                  Why not ?
                  [color=blue]
                  > Any suggestions, comments,
                  > thoughts and links are welcome.[/color]

                  You may want to have a look at JSON. It's 'JavaScript Object Notation' -
                  but Python and javascript are close enough, so the translation process
                  is a no-brainer - and it's used as a replacement for XML in most
                  Pythonic AJAX implementations . One of the potential plus of JSON (vs
                  more python-specific solutions) is that it's language-independant.

                  Else, there was a quite interesting configuration module by Vinay Sajip
                  that could fit your needs:




                  (BTW, is this module still maintained ?)

                  Comment

                  • DH

                    #10
                    Re: Pythonic gui format?

                    Bruno Desthuilliers wrote:[color=blue][color=green]
                    >> I am currently seeking for pythonic alternative for XML.[/color]
                    >
                    > A pretty obvious one is dicts and lists. What about (Q&D):[/color]


                    That's like JSON: http://www.json.org/example.html

                    Comment

                    • bruno at modulix

                      #11
                      Re: Pythonic gui format?

                      DH wrote:[color=blue]
                      > Bruno Desthuilliers wrote:
                      >[color=green][color=darkred]
                      >>> I am currently seeking for pythonic alternative for XML.[/color]
                      >>
                      >>
                      >> A pretty obvious one is dicts and lists. What about (Q&D):[/color]
                      >
                      >
                      > That's like JSON: http://www.json.org/example.html[/color]

                      No, it's pure Python. It happens that JSON looks pretty close to Python,
                      but that's another point.

                      And BTW, you should have read the whole post - I was *also* mentionning
                      JSON as a possible alternative to XML.

                      --
                      bruno desthuilliers
                      python -c "print '@'.join(['.'.join([w[::-1] for w in p.split('.')]) for
                      p in 'onurb@xiludom. gro'.split('@')])"

                      Comment

                      • Christoph Zwerschke

                        #12
                        Re: Pythonic gui format?

                        Bruno Desthuilliers schrieb:[color=blue]
                        > Gregory Petrosyan a écrit :[color=green]
                        >> I am currently seeking for pythonic alternative for XML.[/color]
                        >
                        > A pretty obvious one is dicts and lists. What about (Q&D):
                        >
                        > window = {
                        > 'title' : 'Hello World!'
                        > 'image' : {'text' :"nice picture here",
                        > ...[/color]

                        I think this is pretty much the approach of PythonCard
                        (http://pythoncard.sf.net) with its resource files.

                        Bruno, before writing another simple GUI, have a look at PythonCard.
                        Maybe it already does what you want.

                        -- Christoph

                        Comment

                        • bruno at modulix

                          #13
                          Re: [OT] Pythonic gui format?

                          Christoph Zwerschke wrote:[color=blue]
                          > Bruno Desthuilliers schrieb:
                          >[color=green]
                          >> Gregory Petrosyan a écrit :
                          >>[color=darkred]
                          >>> I am currently seeking for pythonic alternative for XML.[/color]
                          >>
                          >>[/color][/color]
                          (snip)

                          [color=blue]
                          > Bruno, before writing another simple GUI,[/color]

                          <OT>
                          Sorry, Christoph, wrong attribution !-)
                          </OT>

                          --
                          bruno desthuilliers
                          python -c "print '@'.join(['.'.join([w[::-1] for w in p.split('.')]) for
                          p in 'onurb@xiludom. gro'.split('@')])"

                          Comment

                          • Gregory Petrosyan

                            #14
                            Re: Pythonic gui format?

                            > > Isn't it ugly a bit?[color=blue]
                            >I'd even say 'ugly 16-bits' !-)[/color]

                            You are right of course. Those "examples" are really bad, and, most
                            of all, really un-pythonic.

                            Thanks for JSON. It's more clean&simple than XML, but my main idea is
                            to remove any extra layer between Python and GUI. I want all GUI
                            elements/data to be directly accessible from Python (without extra
                            libraries).
                            Your dicts example is nice, but this approach (and some others) lacks
                            one important feature: ordering of GUI elements. In XML, the order of
                            all elements is specified, and with dicts (or with very clean Georg's
                            model) it is not. (BTW remember topics about ordered dicts...)

                            I think that there should be a way for solving this problem, and I'll
                            certainly try to find it.
                            Thanks for your help.

                            Comment

                            • DH

                              #15
                              Re: Pythonic gui format?

                              bruno at modulix wrote:[color=blue]
                              > DH wrote:[color=green]
                              >> Bruno Desthuilliers wrote:
                              >>[color=darkred]
                              >>>> I am currently seeking for pythonic alternative for XML.
                              >>>
                              >>> A pretty obvious one is dicts and lists. What about (Q&D):[/color]
                              >>
                              >> That's like JSON: http://www.json.org/example.html[/color]
                              >
                              > No, it's pure Python. It happens that JSON looks pretty close to Python,
                              > but that's another point.[/color]

                              Python dict and lists ARE JSON. The only difference that python can't
                              handle is multiline comments.

                              Comment

                              Working...