Page 1 of 2 12 Last
  • Jump to page:
    #1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2013
    Location
    EU, USA
    Posts
    25
    Rep Power
    0

    Tkinter color option not working


    The following works fine as expected:

    Code:
    ListBox1.configure(background = "blue")
    OR
    ListBox1['background'] = "#0023FF"
    Then I fetch the setting with gconftool into the variable "option":

    Code:
    option = os.system(CONFTOOL + " --get " + CONFKEY + "background")
    if option != "":
    	print option
    	ListBox1.configure(background = option)
    The string value is fetched correctly but I get error when setting the color:

    Code:
    Traceback (most recent call last):
      File "....", line 129, in <module>
        ListBox1.configure(background = option)
      File "/usr/lib/python2.7/lib-tk/Tkinter.py", line 1205, in configure
        return self._configure('configure', cnf, kw)
      File "/usr/lib/python2.7/lib-tk/Tkinter.py", line 1196, in _configure
        self.tk.call(_flatten((self._w, cmd)) + self._options(cnf))
    _tkinter.TclError: unknown color name "0"

    Any idea why this is happening?
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2010
    Posts
    153
    Rep Power
    5
    What did printing "option" show in this example?
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2013
    Location
    EU, USA
    Posts
    25
    Rep Power
    0
    Originally Posted by admoore
    What did printing "option" show in this example?
    blue
  6. #4
  7. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,997
    Rep Power
    481
    os . system returns the status code.

    int main() {return 0;}

    You need

    os.popen(command).read()


    or use the subprocess module.
    os.popen is easier.
    [code]Code tags[/code] are essential for python code and Makefiles!
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2010
    Posts
    153
    Rep Power
    5
    Originally Posted by b49P23TIvg
    os . system returns the status code.

    int main() {return 0;}

    You need

    os.popen(command).read()


    or use the subprocess module.
    os.popen is easier.
    Good catch.

    I think os.popen is being deprecated. subprocess.check_output() would probably be the simplest approach for the future.
  10. #6
  11. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2013
    Location
    EU, USA
    Posts
    25
    Rep Power
    0
    Originally Posted by b49P23TIvg
    os . system returns the status code.

    int main() {return 0;}

    You need

    os.popen(command).read()


    or use the subprocess module.
    os.popen is easier.
    Thank you. Iím an old hand in C and Visual Basic, but itís my 2nd day with Python, so I donít understand your answer.

    Why does configure() work with a quoted string but not with a variable?
  12. #7
  13. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2013
    Location
    EU, USA
    Posts
    25
    Rep Power
    0
    Oh and another thing: I tried to wrap the above settings routine into a function but python functions apparently cannot accept keywords as parameters. In this case ďbackgroundĒ, ďhighlightcolorĒ, etc, are all keywords. Is there a workaround?
  14. #8
  15. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2010
    Posts
    153
    Rep Power
    5
    Originally Posted by cab2
    Thank you. Iím an old hand in C and Visual Basic, but itís my 2nd day with Python, so I donít understand your answer.

    Why does configure() work with a quoted string but not with a variable?
    Configure() works fine with a variable, your variable is getting the wrong value. os.system does not return the output of the command, it returns the exit status of the command (on POSIX platforms, commands return an integer status, where 0 is success and other values represent different failure states). You are getting "0" because your command succeeded.

    If you want the actual STDOUT output from the command (and you do), you have to use a different python method. We've listed several, but I would suggest using subprocess.check_output, like so:

    Code:
    import subprocess
    option = subprocess.check_output(CONFTOOL + " --get " + CONFKEY + "background")
    # etc...
    Last edited by admoore; August 17th, 2013 at 12:21 AM.
  16. #9
  17. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2010
    Posts
    153
    Rep Power
    5
    Originally Posted by cab2
    Oh and another thing: I tried to wrap the above settings routine into a function but python functions apparently cannot accept keywords as parameters. In this case ďbackgroundĒ, ďhighlightcolorĒ, etc, are all keywords. Is there a workaround?
    Python functions *can* accept keyword arguments, you just have to write the function to do so. It's done like this:

    Code:
    def myfunction(**kwargs):
        somevariable = kwargs.get("somekeyword")
    Any keyword argument you pass in to this function will show up in the kwargs dict, and you can just .get() them by name.
  18. #10
  19. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2013
    Location
    EU, USA
    Posts
    25
    Rep Power
    0
    Originally Posted by admoore
    Configure() works fine with a variable, your variable is getting the wrong value. os.system does not return the output of the command, it returns the exit status of the command (on POSIX platforms, commands return an integer status, where 0 is success and other values represent different failure states). You are getting "0" because your command succeeded.

    If you want the actual STDOUT output from the command (and you do), you have to use a different python method. We've listed several, but I would suggest using subprocess.check_output, like so:

    Code:
    import subprocess
    option = subprocess.check_output(CONFTOOL + " --get " + CONFKEY + "background")
    # etc...
    Thank you. I replaced

    Code:
    option = os.system(CONFTOOL + " --get " + CONFKEY + "background")
    with

    Code:
    option = subprocess.check_output(CONFTOOL + " --get " + CONFKEY + "background")
    and I'm getting error:

    Code:
    Traceback (most recent call last):
      File "/...", line 126, in <module>
        option = subprocess.check_output(CONFTOOL + " --get " + CONFKEY + "background")
      File "/usr/lib/python2.7/subprocess.py", line 537, in check_output
        process = Popen(stdout=PIPE, *popenargs, **kwargs)
      File "/usr/lib/python2.7/subprocess.py", line 679, in __init__
        errread, errwrite)
      File "/usr/lib/python2.7/subprocess.py", line 1249, in _execute_child
        raise child_exception
    OSError: [Errno 2] No such file or directory
    Not bad though, I started with Python for the first time around noon a couple of days ago, and my applet was done by the end of the day.

    I'm just curious, the "print option" line in my OP prints the correct string fetched by "gconftool-2".
  20. #11
  21. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2013
    Location
    EU, USA
    Posts
    25
    Rep Power
    0
    Originally Posted by admoore
    Python functions *can* accept keyword arguments, you just have to write the function to do so. It's done like this:

    Code:
    def myfunction(**kwargs):
        somevariable = kwargs.get("somekeyword")
    Any keyword argument you pass in to this function will show up in the kwargs dict, and you can just .get() them by name.
    Thanks, I'll work on this one. It seems, from googling around, the python is multi-platform. I found and downloaded "Visual Python 2v0.12.2554" but haven't tried it yet. If that's the case, wouldn't it be better to store settings in an .ini file because the Linux config database is not available in Windows?

    The syntax in my OP:

    Code:
    ListBox1['background'] = "#0023FF"
    seems to get around the problem because 'background' is a string and not a keyword?
  22. #12
  23. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2013
    Location
    EU, USA
    Posts
    25
    Rep Power
    0
    Or instead of .ini file, configure via a single delimited string in the command line, e.g. ďblue,green,,300,,Sans,bold,,,Ē where empty tokens mean the some properties do not require configuration and will be set to some default. Simple enough, 1/2 hr work at most? But thereís no strtok() in Python, and re.split() eats empty tokens! Why eat empty tokens?
  24. #13
  25. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2010
    Posts
    153
    Rep Power
    5
    If you are looking for a configuration file format, I'd suggest using one of the many existing formats that already have good library support in Python. There's a .ini parser built in to the standard library, and if you need more functionality than that there's YAML, JSON, XML, and a few other niche formats. I prefer YAML myself.

    As for the os.system / subprocess stuff: The reason your Python script printed "blue" is because the output of the command you run with os.system is echoed to the script's output. If you had removed "print option", you'd still see "blue" in the output.

    You may want to read through the documentation if you plan to continue using subprocess; I think the command and arguments need to be passed in as a list (the command as the first item, each argument/flag as one subsequent item).

    Finally, if you really must have your own custom config file, there is a tokenizer library that does a proper job of tokenizing string.

    http://docs.python.org/2/library/tokenize.html

    Never assume that Python doesn't have something. There are very few facets of programming that aren't covered by Python, and if you want it, chances are somebody else wanted it and created it already. You just have to look around a little.
  26. #14
  27. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2013
    Location
    EU, USA
    Posts
    25
    Rep Power
    0
    Originally Posted by admoore
    If you are looking for a configuration file format, I'd suggest using one of the many existing formats that already have good library support in Python. There's a .ini parser built in to the standard library, and if you need more functionality than that there's YAML, JSON, XML, and a few other niche formats. I prefer YAML myself.

    As for the os.system / subprocess stuff: The reason your Python script printed "blue" is because the output of the command you run with os.system is echoed to the script's output. If you had removed "print option", you'd still see "blue" in the output.

    You may want to read through the documentation if you plan to continue using subprocess; I think the command and arguments need to be passed in as a list (the command as the first item, each argument/flag as one subsequent item).

    Finally, if you really must have your own custom config file, there is a tokenizer library that does a proper job of tokenizing string.

    http://docs.python.org/2/library/tokenize.html

    Never assume that Python doesn't have something. There are very few facets of programming that aren't covered by Python, and if you want it, chances are somebody else wanted it and created it already. You just have to look around a little.
    Thank you. Iím used to finding stuff in MSDN and Python doesnít seem to have an authoritative online reference with examples. It seems all over the place. It doesnít help that Iím coding in a text editor, so I gotta find a proper IDE.

    Iíve 1 last question regarding this ListBox: Iíve set it to interactively increase/decrease font size. But when increasing pt size, the listbox grows and its lower items go out of view. To fix the problem, one has to slightly drag to resize the main window. So Iím looking for a way to invoke the resize event. Iíve searched all over the place, update() doesnít work and I donít want to resort to trickery). Any ideas?
  28. #15
  29. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2010
    Posts
    153
    Rep Power
    5
    Originally Posted by cab2
    Thank you. Iím used to finding stuff in MSDN and Python doesnít seem to have an authoritative online reference with examples. It seems all over the place. It doesnít help that Iím coding in a text editor, so I gotta find a proper IDE.
    The core language and standard library modules should all be documented on docs.python.org. Apart from that, yes, it's a bit spread out. That's the nature of open source, I suppose.

    There are a few projects going trying to unify Python documentation and best practices, but most are young. This is one such effort:

    https://python-guide.readthedocs.org/en/latest/

    Iíve 1 last question regarding this ListBox: Iíve set it to interactively increase/decrease font size. But when increasing pt size, the listbox grows and its lower items go out of view. To fix the problem, one has to slightly drag to resize the main window. So Iím looking for a way to invoke the resize event. Iíve searched all over the place, update() doesnít work and I donít want to resort to trickery). Any ideas?
    Not sure; it may not be possible without manual resizing. Are you using the old style ListBox or the new themeable one?

    Tkinter is nice for simple, quick GUIs since it's built in to the standard library, but it's kind of limited overall. Fortunately there are other options out there.
Page 1 of 2 12 Last
  • Jump to page:

IMN logo majestic logo threadwatch logo seochat tools logo