Mike
Mike

Reputation: 1711

Transient Text in GUI with WXPython

I am working on a simple GUI that outputs a numeric value that is automatically copied to the clipboard. I am trying to get the text "Copied" to flash as soon as this happens, appearing instantly and then disappearing after a second or two.

What happens is the text flashes as I want it to, but the output field that is displayed doesn't update until after the text disappears, which is not the order I have it scripted. The goal would be to have the output field appear to update at the same instant the text appears.

The way I have the text appearing is rather crude, so I am very open to suggestions. Ideas and comments will be appreciated.

import sys
from win32gui import GetWindowText, EnumWindows, ShowWindow, SetForegroundWindow
import win32clipboard
import win32con
import time
import wx
from wxPython.wx import *
import itertools
from time import sleep


class MainFrame(wx.Frame):
    def __init__(self,title):

        wx.Frame.__init__(self, None, title="RRESI Rounder", pos=(0,0), size=(210,160))
        panel=Panel(self)


class Panel(wx.Panel):

    def __init__(self, parent):
        wx.Panel.__init__(self, parent)

        x1=10; x2=110
        y1=10; dy=30; ddy=-3
        boxlength=80

        self.label1 = wx.StaticText(self, label="Input Number:", pos=(x1,y1+dy*1))
        self.Input = wx.TextCtrl(self, value="100.00001", pos=(x2,ddy+y1+dy*1), size=(boxlength,-1))

        self.button =wx.Button(self, label="GO", pos=(9999,y1+dy*3))
        self.Bind(wx.EVT_BUTTON, self.OnClick,self.button)
        self.button.SetDefault()

        self.label0 = wx.StaticText(self, label="Round to closest:  1/", pos=(x1,y1+dy*0))
        self.Denominator = wx.TextCtrl(self, value="64", pos=(x2,ddy+y1+dy*0), size=(boxlength,-1))

        self.label2 = wx.StaticText(self, label="Output Number:", pos=(x1,y1+dy*2))
        self.display = wx.TextCtrl(self, value="100.00000", pos=(x2,ddy+y1+dy*2), size=(boxlength,-1))
        self.display.SetBackgroundColour(wx.Colour(232, 232, 232))

        self.label3 = wx.StaticText(self, label="          ", pos=(x2+7,y1+dy*2+20)) #Copied

        self.label4 = wx.StaticText(self, label="", pos=(x2+7,y1+dy*2))
        self.label4.SetBackgroundColour(wx.Colour(232, 232, 232))

    def OnClick(self,event):      

        def openClipboard():
            try:
                win32clipboard.OpenClipboard()
            except Exception, e:
                print e
                pass

        def closeClipboard():
            try:
                win32clipboard.CloseClipboard()
            except Exception, e:
                print e
                pass

        def clearClipboard():
            try:
                openClipboard()
                win32clipboard.EmptyClipboard()
                closeClipboard()
            except TypeError:
                pass

        def setText(txt): 
            openClipboard()
            win32clipboard.EmptyClipboard()
            win32clipboard.SetClipboardText(txt) 
            closeClipboard()

        Denominator = float(self.Denominator.GetValue())
        Input=float(self.Input.GetValue())
        Output=round(Input*Denominator,0)/Denominator
        self.display.SetValue(str(Output))
        setText(str(Output))
        self.label4.SetLabel(str(Output)+"")
        self.label3.SetLabel("Copied")
        sleep(.5)
        self.label3.SetLabel("                    ")


if __name__=="__main__":
    app = wx.App(redirect=False)   # Error messages don't go to popup window
    frame = MainFrame("RRESI Rounder")
    frame.Show()
    app.MainLoop()

Upvotes: 0

Views: 406

Answers (1)

Joran Beasley
Joran Beasley

Reputation: 114098

you just need to force a redraw otherwise it wont redraw until the next EVT_PAINT is called (after function exits...) by the way sleep is blocking... that means no code is being executed at all while it sleeps

...
self.label3.SetLabel("Copied")
self.Update()#force redraw
....

Upvotes: 1

Related Questions