Output string effect for wxpython

I found a working text effect: http://code.activestate.com/recipes/578292-output-string-with-effect-python3/

I’m trying to create the same effect on a CtrlText when a button is pressed.

import sys
import time

string = “this is a cool test”
s = 0
while s < len(string):
sys.stdout.write(string[s]) ## I have self.changeview as CtrlText
sys.stdout.flush() ## I have self.changeview as CtrlText
time.sleep(0.06) # set the time milliseconds for faster output
s = s + 1
sys.stdout.write(’\n’)

I’ve tried using append, but all I get is a delayed wxpython script.
I need help using this recipe on wxpython.

Hi

I found a working text effect: http://code.activestate.com/recipes/578292-output-string-with-effect-python3/

I’m trying to create the same effect on a CtrlText when a button is pressed.

import sys
import time

string = “this is a cool test”
s = 0
while s < len(string):
sys.stdout.write(string[s]) ## I have self.changeview as CtrlText
sys.stdout.flush() ## I have self.changeview as CtrlText
time.sleep(0.06) # set the time milliseconds for faster output
s = s + 1
sys.stdout.write(‘\n’)

try this

“”"

import wx

class Window(wx.Frame):

def init(self):

super().init(None, -1)

self.SetStatusBar(wx.StatusBar(self))

sizer = wx.BoxSizer()

self.text = wx.TextCtrl(self, -1,

style=wx.TE_MULTILINE|wx.TE_PROCESS_ENTER)

sizer.Add(self.text, 1, wx.EXPAND)

self.SetSizer(sizer)

def f(textctrl, s):

textctrl.SetValue(textctrl.GetValue()+s[0])

wx.CallLater(80, f, textctrl, s[1:])

def main():

a = wx.App(0)

w = Window()

w.Show()

s = ‘cool test indeed…’

wx.CallAfter(f, w.text, s)

a.MainLoop()

if name == ‘main’:

main()

“”"

···

On Sunday, September 15, 2013 10:31:58 PM UTC+3, George McCown wrote:

I’ve tried using append, but all I get is a delayed wxpython script.
I need help using this recipe on wxpython.

Glazner wrote:

Hi

    I found a working text
    effect:Output string with effect, python3+ « Python recipes « ActiveState Code
    <http://code.activestate.com/recipes/578292-output-string-with-effect-python3/&gt;

    I'm trying to create the same effect on a CtrlText when a button is
    pressed.

    import sys
    import time

    string = "this is a cool test"
    s = 0
    while s < len(string):
    sys.stdout.write(string[s]) ## I have self.changeview as CtrlText
    sys.stdout.flush() ## I have self.changeview as CtrlText
    time.sleep(0.06) # set the time milliseconds for faster output
    s = s + 1
    sys.stdout.write('\n')

try this
"""
import wx

class Window(wx.Frame):

def __init__(self):
super().__init__(None, -1)
self.SetStatusBar(wx.StatusBar(self))
sizer = wx.BoxSizer()
self.text = wx.TextCtrl(self, -1,
style=wx.TE_MULTILINE|wx.TE_PROCESS_ENTER)
sizer.Add(self.text, 1, wx.EXPAND)
self.SetSizer(sizer)

def f(textctrl, s):
textctrl.SetValue(textctrl.GetValue()+s[0])
wx.CallLater(80, f, textctrl, s[1:])

def main():
a = wx.App(0)
w = Window()
w.Show()
s = 'cool test indeed...'
wx.CallAfter(f, w.text, s)
a.MainLoop()

if __name__ == '__main__':
main()
"""

Using a timer would work well too, or even just substituting wx.CallLater for wx.CallAfter in your example using a timeout of 60 would more closely approximate the original code.

···

On Sunday, September 15, 2013 10:31:58 PM UTC+3, George McCown wrote:

--
Robin Dunn
Software Craftsman

I’m getting a NameError: name ‘main’ is not defined

Whats going on your last two lines?

···

On Monday, September 16, 2013 1:00:03 PM UTC-5, Glazner wrote:

Hi

On Sunday, September 15, 2013 10:31:58 PM UTC+3, George McCown wrote:

I found a working text effect: http://code.activestate.com/recipes/578292-output-string-with-effect-python3/

I’m trying to create the same effect on a CtrlText when a button is pressed.

import sys
import time

string = “this is a cool test”
s = 0
while s < len(string):
sys.stdout.write(string[s]) ## I have self.changeview as CtrlText
sys.stdout.flush() ## I have self.changeview as CtrlText
time.sleep(0.06) # set the time milliseconds for faster output
s = s + 1
sys.stdout.write(‘\n’)

try this

“”"

import wx

class Window(wx.Frame):

def init(self):

super().init(None, -1)

self.SetStatusBar(wx.StatusBar(self))

sizer = wx.BoxSizer()

self.text = wx.TextCtrl(self, -1,

style=wx.TE_MULTILINE|wx.TE_PROCESS_ENTER)

sizer.Add(self.text, 1, wx.EXPAND)

self.SetSizer(sizer)

def f(textctrl, s):

textctrl.SetValue(textctrl.GetValue()+s[0])

wx.CallLater(80, f, textctrl, s[1:])

def main():

a = wx.App(0)

w = Window()

w.Show()

s = ‘cool test indeed…’

wx.CallAfter(f, w.text, s)

a.MainLoop()

if name == ‘main’:

main()

“”"

I’ve tried using append, but all I get is a delayed wxpython script.
I need help using this recipe on wxpython.

I modified another method. You click a button and the effect works without having to flush the data.

def onChange (self, evt):
    string = " "
    for char in string:
        mybyte = "today I will jump and hop but hope I don't fall and plunge"
        for bit in mybyte:
            self.change_show.write(bit)
            time.sleep(0.04)
        change_show.write("\n")
···

On Wednesday, September 18, 2013 8:35:03 AM UTC-5, George McCown wrote:

I’m getting a NameError: name ‘main’ is not defined

Whats going on your last two lines?
On Monday, September 16, 2013 1:00:03 PM UTC-5, Glazner wrote:

Hi

On Sunday, September 15, 2013 10:31:58 PM UTC+3, George McCown wrote:

I found a working text effect: http://code.activestate.com/recipes/578292-output-string-with-effect-python3/

I’m trying to create the same effect on a CtrlText when a button is pressed.

import sys
import time

string = “this is a cool test”
s = 0
while s < len(string):
sys.stdout.write(string[s]) ## I have self.changeview as CtrlText
sys.stdout.flush() ## I have self.changeview as CtrlText
time.sleep(0.06) # set the time milliseconds for faster output
s = s + 1
sys.stdout.write(‘\n’)

try this

“”"

import wx

class Window(wx.Frame):

def init(self):

super().init(None, -1)

self.SetStatusBar(wx.StatusBar(self))

sizer = wx.BoxSizer()

self.text = wx.TextCtrl(self, -1,

style=wx.TE_MULTILINE|wx.TE_PROCESS_ENTER)

sizer.Add(self.text, 1, wx.EXPAND)

self.SetSizer(sizer)

def f(textctrl, s):

textctrl.SetValue(textctrl.GetValue()+s[0])

wx.CallLater(80, f, textctrl, s[1:])

def main():

a = wx.App(0)

w = Window()

w.Show()

s = ‘cool test indeed…’

wx.CallAfter(f, w.text, s)

a.MainLoop()

if name == ‘main’:

main()

“”"

I’ve tried using append, but all I get is a delayed wxpython script.
I need help using this recipe on wxpython.

George,

try this :

“”"

class Frame(wx.Frame):
def init(self):
super(Frame, self).init(None, -1)
self.text = wx.TextCtrl(self, -1, style=wx.TE_MULTILINE|wx.TE_PROCESS_ENTER)

    val = 'Try this cool effect.....\nHope you will enjoy it...'
    wx.CallAfter(Effect, self.text, val)

def Effect(textctrl, val):
try:
textctrl.SetValue(textctrl.GetValue()+val[0])
wx.CallLater(80, Effect, textctrl, val[1:])
except: pass

if name == ‘main’:
app = wx.App()
Frame().Show()
app.MainLoop()

“”"

This is a modification of Glazner’s program.
This will surely work.

Just curious, haven’t tested the code…, but are you wanting a delayed typewriter-like effect?

Thats exactly right. Navaneeth’s example is the best yet, in my opinion.

···

On Wednesday, September 18, 2013 11:06:52 PM UTC-5, Metallicow wrote:

Just curious, haven’t tested the code…, but are you wanting a delayed typewriter-like effect?

George McCown wrote:

I modified another method. You click a button and the effect works
without having to flush the data.

def onChange (self, evt):
string = " "
for char in string:
mybyte = "today I will jump and hop but hope I don't fall and plunge"
for bit in mybyte:
self.change_show.write(bit)
time.sleep(0.04)
change_show.write("\n")

Even for small amounts of time like this it is still not a good idea to block the event loop. During that 0.04 seconds times the number of items in mybyte the event loop will not be able to send any events, making the UI seem non-responsive to the user. In an event-driven architecture it is always best to use events to signal when it is time to do something, whether that something is responding to some user action or to a change on the system clock or whatever.

···

--
Robin Dunn
Software Craftsman