Guido Carballo-Guerrero | 1 Apr 08:02 2010

Labels not updating its display data

Is it possible to display and update data using labels in a window, created by another window?

This is a program I did that is not working as expected:

Attachment (pro_9.py): text/x-python-script, 1040 bytes

I'm creating a second window inside of a function call newcount. This other window even have separate
variables, but if I use the label type of display, that data is not show, and even less chage its value.

Can somebody tell me if it's possible to do what I'm trying? How can I display a value inside of a label, and
have that display update its values, when this is trying to be done in a second window.

Guido
_______________________________________________
Tkinter-discuss mailing list
Tkinter-discuss <at> python.org
http://mail.python.org/mailman/listinfo/tkinter-discuss
Michael Lange | 1 Apr 12:27 2010
Picon

Re: Labels not updating its display data

Hi Guido,

On Thu, 01 Apr 2010 00:02:36 -0600
Guido Carballo-Guerrero <charras <at> me.com> wrote:

> Is it possible to display and update data using labels in a window,
> created by another window?
> 
> This is a program I did that is not working as expected:
> 
> 
(...)
> def newcount():
> 
>     root2 = Tkt.Tk()
>     var2 = Tkt.IntVar()
>     frameLabels2 = Tkt.Frame(root2)
>     frameLabels2.pack()
> 
>     def changeVal():
> 
>         var2.set(var2.get()+1)
>         print 'var2: ',var2.get()
>         
> 
>     Tkt.Label(frameLabels2,text='var2: ').pack(side=Tkt.LEFT)
>     Tkt.Label(frameLabels2,textvariable=var1).pack(side=Tkt.LEFT)
>     
>     Tkt.Button(root2,text="Change value", command=changeVal).pack()
>     root2.mainloop()
(Continue reading)

Joe Barfett | 2 Apr 15:43 2010
Picon

macPython, menubar won't display, new windows will not display widgets


Thank you for your response! 
Below is some code that works for me in Python 2.5.2 on Windows XP but will not work in Python 2.5.2 on Mac OSX 10.4 or 10.5. 
On the mac, I get a window appearing, but no menu bar. Given that it works on a PC, I'm thinking I need a different Python distro for my macs. 
Thanks very very much!

from Tkinter import *
root = Tk()

def hello():
    print "hello!"

# create a toplevel menu
menubar = Menu(root)
menubar.add_command(label="Hello!", command=hello)
menubar.add_command(label="Quit!", command=root.quit)

# display the menu
root.config(menu=menubar)
mainloop()


--- On Mon, 3/29/10, tkinter-discuss-request <at> python.org <tkinter-discuss-request <at> python.org> wrote:

From: tkinter-discuss-request <at> python.org <tkinter-discuss-request <at> python.org>
Subject: Tkinter-discuss Digest, Vol 73, Issue 5
To: tkinter-discuss <at> python.org
Received: Monday, March 29, 2010, 6:00 AM

Send Tkinter-discuss mailing list submissions to
    tkinter-discuss <at> python.org

To subscribe or unsubscribe via the World Wide Web, visit
    http://mail.python.org/mailman/listinfo/tkinter-discuss
or, via email, send a message with subject or body 'help' to
    tkinter-discuss-request <at> python.org

You can reach the person managing the list at
    tkinter-discuss-owner <at> python.org

When replying, please edit your Subject line so it is more specific
than "Re: Contents of Tkinter-discuss digest..."


Today's Topics:

   1. macPython, menubar won't display,    new windows will not
      display widgets (Joe Barfett)
   2. Re: macPython, menubar won't display, new windows will not
      display widgets (Kevin Walzer)


----------------------------------------------------------------------

Message: 1
Date: Sun, 28 Mar 2010 09:55:29 -0700 (PDT)
From: Joe Barfett <joebarfett <at> yahoo.ca>
To: tkinter-discuss <at> python.org
Subject: [Tkinter-discuss] macPython, menubar won't display,    new
    windows will not display widgets
Message-ID: <470422.35265.qm <at> web59402.mail.ac4.yahoo.com>
Content-Type: text/plain; charset="iso-8859-1"

Hello,
I've had problems getting menus to appear in my Tkinter applications using python 2.5.2 and Mac OSX tiger and leopard.
I've extensively searched for help and found only this link:
http://www.learningpython.com/2006/03/27/using-menus-in-tkinter/
someone else has had the same problem but no solution is posted.
Additionally, I find that if I make a new window with toplevel, widgets on the window will not display immediately, but will after the window is refreshed.
With Tile so easily available, I really don't want to invest in learning another GUI toolkit for python. Tkinter solves all my needs (I'm a scientific programmer) and really I just want to make a more friendly and user oriented scientific application.
Thanks if you can help! I can post source code if needed, but I'm hoping someone has just heard of the problem before and can point me in the right direction. Cheers from Canada!
joe ?




      __________________________________________________________________
Looking for the perfect gift? Give the gift of Flickr!

http://www.flickr.com/gift/
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tkinter-discuss/attachments/20100328/c8567489/attachment-0001.html>

------------------------------

Message: 2
Date: Sun, 28 Mar 2010 13:28:21 -0400
From: Kevin Walzer <kw <at> codebykevin.com>
To: tkinter-discuss <at> python.org
Subject: Re: [Tkinter-discuss] macPython, menubar won't display, new
    windows will not display widgets
Message-ID: <4BAF91B5.5000301 <at> codebykevin.com>
Content-Type: text/plain; charset=ISO-8859-1; format=flowed

On 3/28/10 12:55 PM, Joe Barfett wrote:

> Thanks if you can help! I can post source code if needed, but I'm hoping
> someone has just heard of the problem before and can point me in the
> right direction. Cheers from Canada!
>


There are no systematic issues with Tkinter on OS X that I'm aware of:
if a menu isn't displaying there's probably an issue with your code.
Please post a code sample to help us reproduce the problem.

--Kevin

--
Kevin Walzer
Code by Kevin
http://www.codebykevin.com


------------------------------

_______________________________________________
Tkinter-discuss mailing list
Tkinter-discuss <at> python.org
http://mail.python.org/mailman/listinfo/tkinter-discuss


End of Tkinter-discuss Digest, Vol 73, Issue 5
**********************************************

Yahoo! Canada Toolbar : Search from anywhere on the web and bookmark your favourite sites. Download it now!

_______________________________________________
Tkinter-discuss mailing list
Tkinter-discuss <at> python.org
http://mail.python.org/mailman/listinfo/tkinter-discuss
Guido Carballo-Guerrero | 2 Apr 20:32 2010

Re: Tkinter-discuss Digest, Vol 74, Issue 2

Michael;

Thank you very much. I'm kinda new to Python and Tkinter, and didn't know anything about Toplevel, that was
exactly what I need.

But tell me, why I can't use two instances of Tk? The way I saw this, was that I was just creating a new object, so
I don't understand what is the problem in creating a second object of Tk. I know it doesn't work, but I will
like to understand why.

If you don't know the answer, don't mind. Thank you very much anyway. You really help me on this one.

Charras

On Apr 2, 2010, at 4:00 AM, tkinter-discuss-request <at> python.org wrote:

> Send Tkinter-discuss mailing list submissions to
> 	tkinter-discuss <at> python.org
> 
> To subscribe or unsubscribe via the World Wide Web, visit
> 	http://mail.python.org/mailman/listinfo/tkinter-discuss
> or, via email, send a message with subject or body 'help' to
> 	tkinter-discuss-request <at> python.org
> 
> You can reach the person managing the list at
> 	tkinter-discuss-owner <at> python.org
> 
> When replying, please edit your Subject line so it is more specific
> than "Re: Contents of Tkinter-discuss digest..."
> 
> 
> Today's Topics:
> 
>   1. Re: Labels not updating its display data (Michael Lange)
> 
> 
> ----------------------------------------------------------------------
> 
> Message: 1
> Date: Thu, 1 Apr 2010 12:27:12 +0200
> From: Michael Lange <klappnase <at> web.de>
> To: tkinter-discuss <at> python.org
> Subject: Re: [Tkinter-discuss] Labels not updating its display data
> Message-ID: <20100401122712.60670f79.klappnase <at> web.de>
> Content-Type: text/plain; charset=US-ASCII
> 
> Hi Guido,
> 
> On Thu, 01 Apr 2010 00:02:36 -0600
> Guido Carballo-Guerrero <charras <at> me.com> wrote:
> 
>> Is it possible to display and update data using labels in a window,
>> created by another window?
>> 
>> This is a program I did that is not working as expected:
>> 
>> 
> (...)
>> def newcount():
>> 
>>    root2 = Tkt.Tk()
>>    var2 = Tkt.IntVar()
>>    frameLabels2 = Tkt.Frame(root2)
>>    frameLabels2.pack()
>> 
>>    def changeVal():
>> 
>>        var2.set(var2.get()+1)
>>        print 'var2: ',var2.get()
>> 
>> 
>>    Tkt.Label(frameLabels2,text='var2: ').pack(side=Tkt.LEFT)
>>    Tkt.Label(frameLabels2,textvariable=var1).pack(side=Tkt.LEFT)
>> 
>>    Tkt.Button(root2,text="Change value", command=changeVal).pack()
>>    root2.mainloop()
> 
> There are two problems in this function, first you should _never_ use
> two instances of Tk() in one program; if you need a second window, use
> a Tkinter.Toplevel() instead.
> Second, you assign var1 as textvariable for the Label; I think what you
> actually intended is to set the current value of var1 as initial value
> for var2 and then use var2 as textvariable for the label ? If yes,
> simply change the first lines of newcount() into:
> 
> def newcount():
> 
>    root2 = Tkt.Toplevel()
>    var2 = Tkt.IntVar()
>    var2.set(var1.get())
> 
> I hope this helps
> 
> Michael
> 
> 
> 
> 
> 
> 
> ------------------------------
> 
> _______________________________________________
> Tkinter-discuss mailing list
> Tkinter-discuss <at> python.org
> http://mail.python.org/mailman/listinfo/tkinter-discuss
> 
> 
> End of Tkinter-discuss Digest, Vol 74, Issue 2
> **********************************************
Michael Lange | 2 Apr 21:02 2010
Picon

Re: Tkinter-discuss Digest, Vol 74, Issue 2

Hi Guido,

On Fri, 02 Apr 2010 12:32:03 -0600
Guido Carballo-Guerrero <charras <at> me.com> wrote:

> Michael;
> 
> Thank you very much. I'm kinda new to Python and Tkinter, and didn't
> know anything about Toplevel, that was exactly what I need.
> 
> But tell me, why I can't use two instances of Tk? The way I saw this,
> was that I was just creating a new object, so I don't understand what
> is the problem in creating a second object of Tk. I know it doesn't
> work, but I will like to understand why.
> 

I will try to explain, though I am not a no.1 expert :)

The Tk() window is not just a widget, like a Button or a Toplevel, but
has its own associated Tcl interpreter which is controlled through
Python; so if you have two Tk instances in your application, you have
two different Tcl interpreters running.
As you noticed in your first code example, this did work, but not
exactly as you expected. The reason is that the two Tcl interpreters
cause some unwanted side effects, because they can coexist, but not
interact; for example, (IIRC) you created two IntVars, but IntVar2
inside the second Tcl interpreter, which will not work when trying to
use it as textvariable for a Label belonging to Tcl interpreter #1 ; if
the application becomes more complex, it will obviously become hard to
avoid running into traps like this, when OTOH there is not really a
benefit from the second Tk() window.

I hope this helps

Michael
Cameron Laird | 2 Apr 21:20 2010
Picon

Re: Tkinter-discuss Digest, Vol 74, Issue 2

On Fri, Apr 02, 2010 at 09:02:15PM +0200, Michael Lange wrote:
			.
			.
			.
> > But tell me, why I can't use two instances of Tk? The way I saw this,
> > was that I was just creating a new object, so I don't understand what
> > is the problem in creating a second object of Tk. I know it doesn't
> > work, but I will like to understand why.
> > 
> 
> I will try to explain, though I am not a no.1 expert :)
> 
> The Tk() window is not just a widget, like a Button or a Toplevel, but
> has its own associated Tcl interpreter which is controlled through
> Python; so if you have two Tk instances in your application, you have
> two different Tcl interpreters running.
> As you noticed in your first code example, this did work, but not
> exactly as you expected. The reason is that the two Tcl interpreters
> cause some unwanted side effects, because they can coexist, but not
> interact; for example, (IIRC) you created two IntVars, but IntVar2
> inside the second Tcl interpreter, which will not work when trying to
> use it as textvariable for a Label belonging to Tcl interpreter #1 ; if
> the application becomes more complex, it will obviously become hard to
> avoid running into traps like this, when OTOH there is not really a
> benefit from the second Tk() window.
> 
> I hope this helps
			.
			.
			.
Worse, each Tk() instance opens a "graphical context", and
*their* behavior isn't well-defined beyond one-per-process.
Nemes Andrei | 6 Apr 23:03 2010
Picon

background threading

Hi everyone!
I am trying to make an application in Tkinter that goes to Tray instead of closing. I have programmed the whole interface, including notifications in Tkinter. The problem is, I could not find any library to make the tray icon itself, so I built a tray icon class using wxPython that runs on a separate thread. So basically now, I'm withdrawing the main Tk window, and then showing the tray icon.
The core problem, however, is that while the system tray icon (aka wx main loop) is active, the main program built in Tk completely freezes, including it's notifications. So, my question is, how to make the tray i con thread run in the background, and still have my main program functional?

class trayIcon(object):
    def __init__(self):
        #setup app
        self.app=wx.PySimpleApp()

        #setup icon object
        self.actualIcon = wx.Icon(icon, wx.BITMAP_TYPE_ICO)

        #setup taskbar icon
        self.tbicon = wx.TaskBarIcon()
        self.tbicon.SetIcon(self.actualIcon, name)

        #add taskbar icon event
        wx.EVT_TASKBAR_RIGHT_UP(self.tbicon, self.right)
        wx.EVT_TASKBAR_LEFT_DCLICK(self.t bicon, self.left)

        self.app.MainLoop()      

    def right(self,event):
        global shouldQuit
        self.app.ExitMainLoop()
        shouldQuit=True

    def left(self,event):
        self.app.ExitMainLoop()        
        
class Thread(threading.Thread):
    def __init__(self, main):
        threading.Thread.__init__(self)
        self.setName("wx")
        self.main=main

    def run(self):
        self.Icon=trayIcon()


and here is how I defined it in the main program (must state that this is a class method itself):

def toTray(self):
        global shouldQuit
        self.main.withdraw()
        self.myTray=Thread(self.Main)
        self.myTray.start()
        while self.myTray.isAlive():
            pass

        if shouldQuit:
            self.Quit()
        else:
            self.main.deiconify()

I'm calling the isAlive() method to wait for the wx loop to be ended through it's controls, and then show the main window again; I know this holds up the function in place, but that should not hold the entire program should it?

I must state that using wx to do the entire interface would not be a choice, as I find it a lot harder to use than Tk.

Another solution to the problem would be pointing me to a more convenient tray icon constructor (instructions how to use would be greatly appreciated) if you know any.

I hope someone can help me. Thank you for your time and support.

Regards,
Andrei Nemes

_______________________________________________
Tkinter-discuss mailing list
Tkinter-discuss <at> python.org
http://mail.python.org/mailman/listinfo/tkinter-discuss
Kevin Walzer | 7 Apr 00:37 2010

Re: background threading

On 4/6/10 5:03 PM, Nemes Andrei wrote:

>
> Another solution to the problem would be pointing me to a more
> convenient tray icon constructor (instructions how to use would be
> greatly appreciated) if you know any.
>
> I hope someone can help me. Thank you for your time and support.

This seems a rather convoluted way to do things. There are various Tk 
extensions that provide system tray functionality and it should be 
fairly simple to write a Python wrapper for them.

If you're on Windows, look at winico:

http://wiki.tcl.tk/4089

If you're on *Nix, look at tktray:

http://wiki.tcl.tk/5972

or freedock:

http://wiki.tcl.tk/4090

--Kevin

--

-- 
Kevin Walzer
Code by Kevin
http://www.codebykevin.com
Michael Lange | 7 Apr 11:04 2010
Picon

Re: background threading

Hi,

On Tue, 06 Apr 2010 18:37:42 -0400
Kevin Walzer <kw <at> codebykevin.com> wrote:

> On 4/6/10 5:03 PM, Nemes Andrei wrote:
> 
> >
> > Another solution to the problem would be pointing me to a more
> > convenient tray icon constructor (instructions how to use would be
> > greatly appreciated) if you know any.
> >
> > I hope someone can help me. Thank you for your time and support.
> 
> This seems a rather convoluted way to do things. There are various Tk 
> extensions that provide system tray functionality and it should be 
> fairly simple to write a Python wrapper for them.
> 
> If you're on Windows, look at winico:
> 
> http://wiki.tcl.tk/4089
> 
> If you're on *Nix, look at tktray:
> 
> http://wiki.tcl.tk/5972
> 
> or freedock:
> 
> http://wiki.tcl.tk/4090
> 
> --Kevin
> 

If you want to use tktray, I already wrote a wrapper for it;
to create a tray icon, you can do something like:

root = Tk()
root.withdraw()
img = PhotoImage(file='image.gif')
icon = TkTray.Icon(root)
icon.configure(image=img)
icon.menu.add_command(label='Quit', command=root.quit)

HTH

Michael

######## file TkTray.py #####################################

import Tkinter

class Icon(Tkinter.BaseWidget, Tkinter.Wm):
    def __init__(self, master=None, cnf={}, **kw):

        if not master:
            if Tkinter._support_default_root:
                if not Tkinter._default_root:
                    Tkinter._default_root = Tkinter.Tk()
                master = Tkinter._default_root
        self.TktrayVersion = master.tk.call('package', 'require', 'tktray')

        # stolen from Tkinter.Toplevel
        if kw:
            cnf = Tkinter._cnfmerge((cnf, kw))
        extra = ()
        for wmkey in ['screen', 'class_', 'class', 'visible', 'colormap']:
            if cnf.has_key(wmkey):
                val = cnf[wmkey]
                # TBD: a hack needed because some keys
                # are not valid as keyword arguments
                if wmkey[-1] == '_': opt = '-'+wmkey[:-1]
                else: opt = '-'+wmkey
                extra = extra + (opt, val)
                del cnf[wmkey]
        Tkinter.BaseWidget.__init__(self, master, 'tktray::icon', cnf, {}, extra)
        self.protocol("WM_DELETE_WINDOW", self.destroy)

        self.menu = Tkinter.Menu(self, tearoff=0)
        self.bind('<3>', self._popupmenu)

    def bbox(self):
        return self._getints(self.tk.call(self._w, 'bbox')) or None

    def _popupmenu(self, event):
        w, h = self.menu.winfo_reqwidth(), self.menu.winfo_reqheight()
        x0, y0, x1, y1 = self.bbox()
        # get the coords for the popup menu; we want it to the mouse pointer's
        # left and above the pointer in case the taskbar is on the bottom of the
        # screen, else below the pointer; add 1 pixel towards the pointer in each
        # dimension, so the pointer is '*inside* the menu when the button is being
        # released, so the menu will not unpost on the initial button-release event
        if y0 > self.winfo_screenheight() / 2:
            # assume the panel is at the bottom of the screen
            x, y = event.x_root - w + 1, event.y_root - h + 1
        else:
            x, y = event.x_root - w + 1, event.y_root - 1
        # make sure that x is not outside the screen
        if x < 5:
            x = 5
        self.menu.tk_popup(x, y)

#################################################################
Michael Lange | 13 Apr 23:13 2010
Picon

Re: background threading

Hi,

On Wed, 7 Apr 2010 08:00:38 -0700 (PDT)
Nemes Andrei <teh_sh_meister <at> yahoo.com> wrote:

> I'm sorry for being such a beginner in this, but could not find where
> to download the winico. Is it included in Tk already? 

I think you will have to download winico-0.6.zip from

    http://sourceforge.net/projects/tktable/files/

and copy the winico-0.6 folder into your Python's Tcl folder so that Tk
can find it. Unfortunately there does not seem to be a Tkinter wrapper
available, so you will have to write it yourself :(

>From a quick glance at the winico manpage a quick'n'dirty wrapper could
look like this (untested!):
############# file Winico.py ##############################
import Tkinter

class Winico:
    def __init__(self, filename):

        master = Tkinter._default_root
        if not master:
            raise RuntimeError, 'Too early to create icon'
        self.WinicoVersion = master.tk.call('package', 'require', 'winico')
        self.tk = master.tk

    def createfrom(self, filename):
        return self.tk.call('winico', 'createfrom', filename)

    def delete(self, id_):
        self.tk.call('winico', 'delete', id_)

    def load(self, resourcename, filename=None):
        return self.tk.call('winico', 'load', resourcename, filename)

    def info(self, id_=None):
        # the output should of course be formatted somehow
        return self.tk.call('winico', 'info', id_)

    def setwindow(self, size='big', pos=None):
        return self.tk.call('winico', 'setwindow', size, pos)

    def taskbar_add(self, id_, callback=None, pos=None, text=None):
        args = ()
        if callback:
            args += ('-callback', callback)
        if pos:
            args += ('-pos', pos)
        if text:
            args += ('-text', text)
        return self.tk.call('winico', 'taskbar', 'add', id_, *args)

    def taskbar_modify(self, id_, callback=None, pos=None, text=None):
        args = ()
        if callback:
            args += ('-callback', callback)
        if pos:
            args += ('-pos', pos)
        if text:
            args += ('-text', text)
        return self.tk.call('winico', 'taskbar', 'modify', id_, *args)

    def taskbar_delete(self, id_):
        return self.tk.call('winico', 'taskbar', 'delete', id_)

#########################################################

If you compare this with the winico man page from

  http://tktable.sourceforge.net/winico/winico.html

you will probably see the point how this is supposed to work.

Another problem is how to write the callback function for mouse clicks
onto the icon. I guess you will have to add the percent substitutions
and register the callback manually, like:

    import Tkinter, Winico
    root = Tkinter.Tk()

    def your_callback(icon_id, message_specifier):
        if message_specifier == "WM_LBUTTONDBLCLK":
            (... do something with icon_id...)
        elif message_specifier == "WM_RBUTTONDOWN":
            (... do something else with icon_id...)

    # register the callback for Tk:
    cmd = (root.register(your_callback), '%i', '%m')

    # create an icon
    icon = Winico.Winico()
    icon_id = icon.createfrom("some.ico")
    icon.taskbar_add(icon_id, callback=cmd, text="Your text here")

I think you see the point, how the percent substitutions are passed to
the callback.

I hope this helps

Michael

PS:
I forwarded this to the list, maybe it is interesting for
other people, too, I hope that's ok.

Gmane