[Tkinter-discuss] Fwd: WxPython -> Tkinter

Guilherme Polo ggpolo at gmail.com
Sun Nov 2 18:56:13 CET 2008


On Sun, Nov 2, 2008 at 3:26 PM, Olrik Lenstra <o.lenstra at gmail.com> wrote:
> I see. So I will have to go about and change all the Tk.Button() to
> ttk.Button()?
> The program I am making will be for the windows OS only.

What I'm saying is that you won't even need ttk widgets for this, if
you stick to windows only.

>
> Is the syntax for ttk the same as for Tkinter?

ttk is still tcl/tk, it is just new widges and theming engine. If by
same syntax you mean the widgets support the same options and methods,
then no.

>
> Regards,
> Olrik
>
> 2008/11/2 Guilherme Polo <ggpolo at gmail.com>:
>> On Sun, Nov 2, 2008 at 12:34 PM, Olrik Lenstra <o.lenstra at gmail.com> wrote:
>>> I would like to thank you already for all the help you've given me, it
>>> is really appreciated :)
>>
>> You are welcome.
>>
>>> I decided to update to Python2.6 instead of using the tile pack. My
>>> application now shows the GUI again.
>>> So I added the code you gave me to prevent the window from hanging
>>> once I execute my scan.
>>> I get the following Traceback:
>>>
>>> D:\Documents\OLPrograms\TroubleShooting Olrik\sourcecode\TSO(source)>python TSO.
>>> pyw
>>> Exception in Tkinter callback
>>> Traceback (most recent call last):
>>>  File "D:\Python26\lib\lib-tk\Tkinter.py", line 1410, in __call__
>>>    return self.func(*args)
>>>  File "D:\Documents\OLPrograms\TroubleShooting Olrik\sourcecode\TSO(source)\TSO
>>> main.pyw", line 29, in OnScan
>>>    TSOscn.Scan(root, status)
>>>  File "D:\Documents\OLPrograms\TroubleShooting Olrik\sourcecode\TSO(source)\TSO
>>> scn.pyw", line 23, in Scan
>>>    TSOex.safe_yield(Frame, True)
>>>  File "D:\Documents\OLPrograms\TroubleShooting Olrik\sourcecode\TSO(source)\TSO
>>> ex.pyw", line 75, in safe_yield
>>>    window_disabler(window)
>>>  File "D:\Documents\OLPrograms\TroubleShooting Olrik\sourcecode\TSO(source)\TSO
>>> ex.pyw", line 90, in window_disabler
>>>    if widget.instate(['!disabled']):
>>> AttributeError: Button instance has no attribute 'instate'
>>>
>>
>> That is because Button is not a ttk.Button, but a "normal"
>> Tkinter.Button. I didn't know before you would be using windows, but
>> since this is the case, you may try substituting the use of instate
>> and state calls (that are available only for ttk widgets) by the use
>> of widget.wm_attributes('-disabled', 1),
>> widget.wm_attributes('-disabled', 0) and
>> widget.wm_attributes('-disabled'). I will be able to test it here
>> later since my new pc arrived with a windows vista.
>>
>>> D:\Documents\OLPrograms\TroubleShooting Olrik\sourcecode\TSO(source)>
>>>
>>> I don't know if it makes a difference, But I think you should know
>>> that I use different files that import other applications (made by me)
>>> Example:
>>>
>>> TSO.pyw is the main script, this looks if the very first argument is
>>> "TSO.pyw", if it is, run "TSOmain.TSO()"
>>> TSOmain.pyw is the GUI and the GUI only, from there it calls other files etc.
>>> I hope that wasn't too confusing.
>>>
>>> Thanks again, I really appreciate it.
>>>
>>> Regards,
>>> Olrik
>>>
>>> 2008/11/2 Guilherme Polo <ggpolo at gmail.com>:
>>>> On Sun, Nov 2, 2008 at 10:34 AM, Olrik Lenstra <o.lenstra at gmail.com> wrote:
>>>>> Hmm, I'm a bit stumped.
>>>>> I installed the ttk module and tried applying the code.
>>>>> However when I ran my code it gave me an error.
>>>>> I thought I might have left a small typo in or something and removed
>>>>> the code to check my own.
>>>>>
>>>>> After I removed the ttk import and code it wouldn't even run my own code again.
>>>>> Below is the Traceback:
>>>>>
>>>>> C:\Users\Olrik Lenstra\Desktop\TroubleShooting Olrik>python TSO.pyw
>>>>> Traceback (most recent call last):
>>>>>  File "TSO.pyw", line 24, in <module>
>>>>>    import TSOmain
>>>>>  File "C:\Users\Olrik Lenstra\Desktop\TroubleShooting Olrik\TSOmain.pyw", line
>>>>> 79, in <module>
>>>>>    TSO()
>>>>>  File "C:\Users\Olrik Lenstra\Desktop\TroubleShooting Olrik\TSOmain.pyw", line
>>>>> 37, in TSO
>>>>>    root = Tk.Tk()
>>>>>  File "D:\Python25\lib\lib-tk\Tkinter.py", line 1638, in __init__
>>>>>    self._loadtk()
>>>>>  File "D:\Python25\lib\site-packages\ttk.py", line 50, in _wrapper
>>>>>    self.tk.eval('package require tile') # TclError may be raised here
>>>>> _tkinter.TclError: can't find package tile
>>>>>
>>>>
>>>> You don't have tile installed neither was your tkinter compiled
>>>> against tcl/tk 8.5.
>>>> Python 2.6 already brings tcl/tk 8.5 and tkinter compiled against them.
>>>> Another option is to download tile from:
>>>> http://sourceforge.net/project/showfiles.php?group_id=11464&package_id=107795
>>>> (get the tile082.zip so you have to do nothing at all). Then unpack
>>>> that somewhere. Then you have to set the environment variable
>>>> TILE_LIBRARY to the directory where this was unpacked, then you should
>>>> be able to run the program with ttk.
>>>> You could set this environment var inside your app too:
>>>>
>>>> import os
>>>> os.environ['TILE_LIBRARY'] = 'x:/unpacked/tile/here'
>>>>
>>>> import ttk
>>>>
>>>> ....
>>>>
>>>>> C:\Users\Olrik Lenstra\Desktop\TroubleShooting Olrik>
>>>>>
>>>>>
>>>>> Regards,
>>>>> Olrik
>>>>>
>>>>> 2008/10/29 Olrik Lenstra <o.lenstra at gmail.com>:
>>>>>> So that means that you will use a ttk frame instead of the Tkinter.Frame?
>>>>>> I'll see if I can get this working once I get home. (my program is on my
>>>>>> Desktop and I just do some testing on my laptop)
>>>>>>
>>>>>> Thanks a lot so far!
>>>>>> Regards,
>>>>>> Olrik
>>>>>>
>>>>>> 2008/10/29 Guilherme Polo <ggpolo at gmail.com>
>>>>>>>
>>>>>>> On 10/29/08, Olrik Lenstra <o.lenstra at gmail.com> wrote:
>>>>>>> > I see, Thanks a lot, I really don't wish to bother you any further, but
>>>>>>> > here's my current situation.
>>>>>>> > I am still a beginning programmer and I am not entirely sure where to
>>>>>>> > put
>>>>>>> > this code exactly.
>>>>>>> >
>>>>>>> > How would I go about using this code?
>>>>>>> >
>>>>>>>
>>>>>>> It should be very similar to what you are already doing in wx.
>>>>>>>
>>>>>>> But, you would need to layout your toplevel (the one that is created
>>>>>>> when you call Tkinter.Tk()) as this:
>>>>>>>
>>>>>>> There would be a ttk.Frame that would hold all the other widgets,
>>>>>>> which should be all ttk widgets according to this sample. Then you
>>>>>>> would call safe_yield(frame, True) in the same situations you would in
>>>>>>> wx. Now it remains to check if there is the same need for this in tk
>>>>>>> as there is in wx.
>>>>>>>
>>>>>>> Finally, a sample way to layout the widgets:
>>>>>>>
>>>>>>> root = Tkinter.Tk()
>>>>>>> frame = ttk.Frame(root)
>>>>>>> btn1 = ttk.Button(frame, text="Button 1")
>>>>>>> ...
>>>>>>> ... some time later:
>>>>>>> safe_yield(frame, True)
>>>>>>> ...
>>>>>>>
>>>>>>> > Thank you so much in advance.
>>>>>>> > Regards,
>>>>>>> > Olrik
>>>>>>> >
>>>>>>> >  2008/10/29 Guilherme Polo <ggpolo at gmail.com>
>>>>>>> >
>>>>>>> > >
>>>>>>> > >
>>>>>>> > >
>>>>>>> > > ---------- Forwarded message ----------
>>>>>>> > > From: Guilherme Polo <ggpolo at gmail.com>
>>>>>>> > > Date: Oct 29, 2008 9:16 AM
>>>>>>> > > Subject: Re: [Tkinter-discuss] WxPython -> Tkinter
>>>>>>> > > To: Olrik Lenstra <o.lenstra at gmail.com>
>>>>>>> > > Cc: python-list at python.org
>>>>>>> > >
>>>>>>> > >
>>>>>>> > > On 10/29/08, Olrik Lenstra <o.lenstra at gmail.com> wrote:
>>>>>>> > >  > Hello everyone,
>>>>>>> > >  >
>>>>>>> > >  > A while ago I joined the Tutor mailing list, and they helped me out
>>>>>>> > with a
>>>>>>> > >  > question regarding wxPython.
>>>>>>> > >  > Now however, I have tried a program in Tkinter and I would like to
>>>>>>> > > see
>>>>>>> > if
>>>>>>> > >  > there is a similar command to "wx.SafeYield(self, True)".
>>>>>>> > >
>>>>>>> > >
>>>>>>> > > It will be a combination of commands, not a single one. Initially I
>>>>>>> > >  considered this as "probably without solution", since tcl acquired a
>>>>>>> > >  yield command just in the 8.6a3 release, but then I looked at
>>>>>>> > >  wx.SafeYield code and apparently it is possible to replicate it.
>>>>>>> > >
>>>>>>> > >  Here is an initial cut, it is very possible to contain something not
>>>>>>> > >  equivalent to wx.SafeYield (besides it could be improved):
>>>>>>> > >
>>>>>>> > >
>>>>>>> > >  import ttk
>>>>>>> > >
>>>>>>> > >  inside_tkyield = False
>>>>>>> > >  disabled_wins = {}
>>>>>>> > >
>>>>>>> > >  def safe_yield(window, only_if_needed=False):
>>>>>>> > >    window_disabler(window)
>>>>>>> > >
>>>>>>> > >    try:
>>>>>>> > >        return tk_yield(window, only_if_needed)
>>>>>>> > >    finally:
>>>>>>> > >        for widget, flags in disabled_wins.iteritems():
>>>>>>> > >            ttk.Widget.state(widget, flags)
>>>>>>> > >        disabled_wins.clear()
>>>>>>> > >
>>>>>>> > >
>>>>>>> > >  def window_disabler(window):
>>>>>>> > >    widgets = window.children.values()
>>>>>>> > >    widgets.append(window)
>>>>>>> > >
>>>>>>> > >    for widget in widgets:
>>>>>>> > >        if widget.instate(['!disabled']):
>>>>>>> > >            prev_flags = widget.state(['disabled'])
>>>>>>> > >            disabled_wins[widget] = prev_flags
>>>>>>> > >
>>>>>>> > >
>>>>>>> > >  def tk_yield(window, only_if_needed=False):
>>>>>>> > >    # wx implements this differently based on the backend it is using
>>>>>>> > >    global inside_tkyield
>>>>>>> > >    if inside_tkyield:
>>>>>>> > >        if not only_if_needed:
>>>>>>> > >            raise RuntimeError("safe_yield called recursively")
>>>>>>> > >
>>>>>>> > >        return False
>>>>>>> > >
>>>>>>> > >    inside_tkyield = True;
>>>>>>> > >
>>>>>>> > >    window.update()
>>>>>>> > >    window.update_idletasks()
>>>>>>> > >
>>>>>>> > >    inside_tkyield = False;
>>>>>>> > >
>>>>>>> > >    return True
>>>>>>> > >
>>>>>>> > >
>>>>>>> > >  Note that this depends on ttk widgets
>>>>>>> > >  (http://pypi.python.org/pypi/pyttk) since it uses
>>>>>>> > widget.state to
>>>>>>> > >  disable and reenable the widgets. On windows the "wm" command
>>>>>>> > > supports
>>>>>>> > >  disabling the entire window, so it is easier if you can use it.
>>>>>>> > >
>>>>>>> > > [Forwarded because I sent to the wrong list first time]
>>>>>>> > >
>>>>>>> > >
>>>>>>> > >  >  Below is a copy of the message to the tutor list.
>>>>>>> > >  >
>>>>>>> > >  > > Dear Mailing list,
>>>>>>> > >  > >
>>>>>>> > >  > > a while ago a few of you helped me solve an issue I had with a
>>>>>>> > > GUI /
>>>>>>> > scan
>>>>>>> > >  > > program that I made.
>>>>>>> > >  > > The problem was that when I tried to move the frame it would hang
>>>>>>> > until
>>>>>>> > >  > the
>>>>>>> > >  > > scan was finished.
>>>>>>> > >  > > To solve this I had to add "wx.SafeYield(self, True)" to the scan
>>>>>>> > > and
>>>>>>> > the
>>>>>>> > >  > > GUI wouldn't hang any more.
>>>>>>> > >  > > Now I have redone the program and have written it with Tkinter
>>>>>>> > instead of
>>>>>>> > >  > > WxPython.
>>>>>>> > >  > >
>>>>>>> > >  > > So is there a similar command for Tkinter as there is for
>>>>>>> > > WxPython?
>>>>>>> > >  > >
>>>>>>> > >  > > Thanks in advance.
>>>>>>> > >  > > Regards,
>>>>>>> > >  > > Olrik
>>>>>>> > >  > >
>>>>>>> > >  >
>>>>>>> > >
>>>>>>> > > > _______________________________________________
>>>>>>> > >  >  Tkinter-discuss mailing list
>>>>>>> > >  >  Tkinter-discuss at python.org
>>>>>>> > >  >
>>>>>>> > http://mail.python.org/mailman/listinfo/tkinter-discuss
>>>>>>> > >  >
>>>>>>> > >  >
>>>>>>> > >
>>>>>>> > >
>>>>>>> > >
>>>>>>> > >  --
>>>>>>> > >  -- Guilherme H. Polo Goncalves
>>>>>>> > >
>>>>>>> > >
>>>>>>> > > --
>>>>>>> > > -- Guilherme H. Polo Goncalves
>>>>>>> > >
>>>>>>> > >
>>>>>>> > >
>>>>>>> > > _______________________________________________
>>>>>>> > > Tkinter-discuss mailing list
>>>>>>> > > Tkinter-discuss at python.org
>>>>>>> > >
>>>>>>> > http://mail.python.org/mailman/listinfo/tkinter-discuss
>>>>>>> > >
>>>>>>> >
>>>>>>> >
>>>>>>>
>>>>>>>
>>>>>>> --
>>>>>>> -- Guilherme H. Polo Goncalves
>>>>>>
>>>>>>
>>>>>
>>>>
>>>>
>>>>
>>>> --
>>>> -- Guilherme H. Polo Goncalves
>>>>
>>>
>>
>>
>>
>> --
>> -- Guilherme H. Polo Goncalves
>>
>



-- 
-- Guilherme H. Polo Goncalves


More information about the Tkinter-discuss mailing list