How to use 2to3.py conversion, output text to scrollable window

Updated TrustyTony 0 Tallied Votes 764 Views Share

We can run Python2 script in most simple cases if we just use the 2to3.py script found in recent Python versions:

F:\Python27\Tools\Scripts>copy \test\output_window_tk.py \test\output_window_tk2.py
1 tiedosto(a) on kopioitu.

F:\Python27\Tools\Scripts>2to3.py  -w \test\output_window_tk.py
RefactoringTool: Skipping implicit fixer: buffer
RefactoringTool: Skipping implicit fixer: idioms
RefactoringTool: Skipping implicit fixer: set_literal
RefactoringTool: Skipping implicit fixer: ws_comma
RefactoringTool: Refactored \test\output_window_tk.py
--- \test\output_window_tk.py   (original)
+++ \test\output_window_tk.py   (refactored)
@@ -1,5 +1,5 @@
-from ScrolledText import ScrolledText
-from Tkinter import BOTH, YES, END
+from tkinter.scrolledtext import ScrolledText
+from tkinter import BOTH, YES, END

 fn = 'f:/Python27/setuptools-wininst.log'
 mywin = ScrolledText()
RefactoringTool: Files that were modified:
RefactoringTool: \test\output_window_tk.py

Here we backed up the original version to another name as we want to be able to continue running the script in Python2, and -w overwrites the original file. It does produce backup file, but that has not .py extension and backups are not for using but for backup.

We edit the docstring and the result is:

""" Produce an output window showing the file fn,
    replace file opening part with simple insert,
    by replacing the f.read() with string name (and removing fn/with stuff)
    if the string is in memory

    This script was produced from Python2 script by the 2to3.py conversion script.

"""

from tkinter.scrolledtext import ScrolledText
from tkinter import BOTH, YES, END

fn = 'f:/Python27/setuptools-wininst.log'
mywin = ScrolledText()
mywin.pack(fill=BOTH, expand=YES)
with open(fn) as f:
     mywin.insert(END,f.read())

mywin.mainloop()

If you want to run the script without bringing up the console output, you can change the extension to .pyw to run the script with pythonw.exe.

""" Produce an output window showing the file fn,
    replace file opening part with simple insert,
    by replacing the f.read() with string name (and removing fn/with stuff)
    if the string is in memory
"""

from ScrolledText import ScrolledText
from Tkinter import BOTH, YES, END


fn = 'f:/Python27/setuptools-wininst.log'
mywin = ScrolledText()
mywin.pack(fill=BOTH, expand=YES)
with open(fn) as f:
     mywin.insert(END,f.read())

mywin.mainloop()