Skip to content

Instantly share code, notes, and snippets.

@balachandrana
Last active February 9, 2019 16:21
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save balachandrana/6c8617382079e4486b12a4dd35f5c8c5 to your computer and use it in GitHub Desktop.
Save balachandrana/6c8617382079e4486b12a4dd35f5c8c5 to your computer and use it in GitHub Desktop.
redirect_console_output.py
import ui
import sys
class TextViewFile(object):
def __init__(self, textview):
self.textview = textview
def write(self, msg):
self.textview.text += msg
scroll()
backup_stdout = None
def button_action(sender):
print(sender.title + ' pressed')
def segmentedcontrol_action(sender):
global backup_stdout
if sender.selected_index == 1:
if not backup_stdout:
backup_stdout = sys.stdout
sys.stdout = textview_stdout
else:
if backup_stdout:
sys.stdout = backup_stdout
backup_stdout = None
class CloseView(ui.View):
def __init__(self):
super().__init__()
def will_close(self):
global backup_stdout
sys.stdout = backup_stdout
backup_stdout = None
v = ui.load_view()
v['textview1'].text = '''
****************
Button press events
are logged in console
or TextView based on
respective tab
selection
*****************
'''
textview_stdout = TextViewFile(v['textview1'])
v['segmentedcontrol1'].selected_index = 0
v['segmentedcontrol1'].action = segmentedcontrol_action
@ui.in_background
def scroll():
v['textview1'].content_offset = (0, v['textview1'].content_size[1] -v['textview1'].height)
clv = CloseView()
clv.width, clv.height = v.width, v.height
clv.add_subview(v)
clv.present('sheet')
[
{
"class" : "View",
"attributes" : {
"background_color" : "RGBA(1.000000,1.000000,1.000000,1.000000)",
"tint_color" : "RGBA(0.000000,0.478000,1.000000,1.000000)",
"enabled" : true,
"border_color" : "RGBA(0.000000,0.000000,0.000000,1.000000)",
"flex" : ""
},
"frame" : "{{0, 0}, {451, 300}}",
"selected" : false,
"nodes" : [
{
"class" : "Button",
"attributes" : {
"font_size" : 15,
"title" : "Button1",
"name" : "button1",
"action" : "button_action",
"class" : "Button",
"frame" : "{{80, 104}, {80, 32}}",
"uuid" : "5C1173E2-1C48-4FC7-AED9-2256DCBADDA4"
},
"frame" : "{{56, 220}, {141, 52}}",
"selected" : false,
"nodes" : [
]
},
{
"class" : "TextView",
"attributes" : {
"font_size" : 17,
"font_name" : "<System>",
"flex" : "WH",
"autocorrection_type" : "default",
"name" : "textview1",
"spellchecking_type" : "default",
"class" : "TextView",
"alignment" : "left",
"frame" : "{{20, 20}, {200, 200}}",
"editable" : false,
"uuid" : "4B000022-3FCC-40CE-A1B8-56F8ED7C996F"
},
"frame" : "{{279, 77}, {152, 195}}",
"selected" : false,
"nodes" : [
]
},
{
"class" : "Button",
"attributes" : {
"font_size" : 15,
"title" : "Button2",
"name" : "button2",
"action" : "button_action",
"class" : "Button",
"frame" : "{{186, 134}, {80, 32}}",
"uuid" : "822A696E-6FB8-4421-8BFC-C8C6B63B6EC2"
},
"frame" : "{{56, 105}, {135, 58}}",
"selected" : true,
"nodes" : [
]
},
{
"class" : "SegmentedControl",
"attributes" : {
"class" : "SegmentedControl",
"name" : "segmentedcontrol1",
"frame" : "{{166, 136}, {120, 29}}",
"uuid" : "384FC3D2-00A5-41AC-9B07-507B8DBD0150",
"segments" : "console|textview",
"flex" : "LR"
},
"frame" : "{{24, 26}, {407, 28}}",
"selected" : false,
"nodes" : [
]
}
]
}
]
@balachandrana
Copy link
Author

fixed bugs related to scrolling and sys.stdout not reverted back while closing

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment