I am making a notepad using Tkinter and I want to be able to print to the printer the notes taken.I am using a Text object in order to allow the user to write his notes.I couldn't find anything on to how to print to printer with Tkinter and I want to avoid using PyQT or win32api that I know have printer support(due to complexity and lack of experience).I am planning on releasing the application only on windows so I donnot need the it to be cross-platform.
Fact to Accept: Cross-Platform Printing is hard.
Depending on the system you use, the commands to send text / files to a printer will be very different.
For Windows, you should probably use the Python win32print module, even if you say you don't want to use it because of complexity etc.
I honestly think that trying to solve it any other way would be a lot more complicated in the end.
For Linux, Mac, Unix, you can send commands much more directly using LPR to the system printer through the built-in os.popen() or the new subprocess module in Python, but for Windows, my bet is you're better off using the win32print module.
Providing cross-platform printing functionality will always be a challenge because of the differences in the underlying sub-systems on different operating systems.
The basic approach
You'll need to separate the logic of your code, so that depending on the underlying OS, your program will choose the right method for executing the printing functionality you need.
I don't know of any way around this.
Using the Python win32 modules needn't be as complex as you might think.
For Windows
This can be done with the module win32print,
Documented nicely here
For Linux, macOS, Unix
Check out the use of LPR commands, and combine this with basic Python os.popen calls or using the new Python subprocess module
I know you probably wanted a more "copy/paste friendly" way, but that would be very hard without having the rest of your code and not knowing the exact requirements / specs for your app.
Bottom line is that you'd probably end up writing custom code for each platform for printing anyway, so might as well jump in head first.
Related
Is there a newer way to open a PDF using an external viewer from Python 3 in Linux other than subprocess?
This sounds like a noobish and duplicate question, but I looked at this question and this question, and all of the answers are over 7 years old and recommended discouraged methods like os.system, old methods like manually creating a subprocess.Popen or Windows-only methods like os.startfile.
So in the time since these questions were answered, have preferred methods of launching a PDF reader from within Python emerged, or are these still the best answers?
Python as of 3.6 still doesn't have a cross-platform way to open files using default programs.
Issue 3177 suggested to add one, but it didn't happen yet.
So:
On Windows, there's a system call for this, you can reach it from Python via os.startfile,
On Linux, there's a command-line tool called xdg-open that does this,
On Mac OS, there's a command-line tool simply called open.
This means that unfortunately you still need to check the operating system and pick the right approach. The correct way to call the command-line tools is using the subprocess module.
This answer provides a code snippet:
Open document with default application in Python
I want to develop a small Python app that interacts with the user via the console/command line. Are there any good libraries I can use to get user input and display the output in a nice-looking way? I tried searching but most of what I found was command-line argument processing, didn't see anything about user interaction in the shell.
It should be crossplatform (Windows and Linux)
A really excellent library is cmd which is part of the python standard library. It is cross platform Windows, Linux, Mac. You just have to implement one class and it offers so many great features:
provides list of supported commands(they end up being implemented as methods)
help command that can be called to explain one of your commands
handles the whole entering a command, checking syntax, and calling your method command loop cycle.
users can save the commands they have run in an interactive session and run them as a script. check out the example below.
If you take the turtle shell example and save it as turtleshell.py and save the below turtle script file as circles.txt
circle 20
circle 50
circle 100
bye
then you could run the turtle script with the following command:
cat circles.txt | ./turtleshell.py
so in the simple example shown in the docs the developer has essentially made a simple mini-language that can be used as an easier interface to to the turtle module making it even easier to introduce programming to kids. The examples above have been taken from the python3 docs because they have included a detailed example in their docs which wasn't there in the 2.7 docs, but cmd is available and fully functional in python 2.3 and later.
You can control the Unix terminal with the curses library. The library essentially lets you build a simple terminal GUI.
If you need more, take a look at Urwid as well. Urwid offers more complex GUI widgets for the discerning terminal GUI developer. :-)
Curses is the most widely used in the Unix environment according to the doc. For Windows you could look at Windows Console Driver, WConio - Windows CONsole I/O for Python or Wcurses. I couldn't find much on cross platform console libraries unfortuntaly.
If your Windows users are CLI users they probably have cygwin which has ncurse support so curses is still the best option if you ask me.
I need to send code to remote clients to be executed in them but security is a concern for me right now. I don't want unsafe code to be executed there so I would like to control what a program is doing. I mean for example, know if is making connections, where is connecting to, if is reading local files, etc. Is this possible with Python?
EDIT: I'm thinking in something similar to Android permission system. I want to know what a code will do and if it does something different, stop it.
You could use a different Python runtime:
if you run your script using Jython; you can exploit Java's permission system
with Pypy's sandboxed version you can choose what is allowed to run in your controller script
There used to be a module in Python called bastian, but that was deprecated as it wasn't that secure. There's also I believe something called RPython, but I don't know too much about that.
I would in this case use Pyro and write the code on the target server. That way you know clients can only execute written and tested code.
edit - it's probably worth noting that Pyro also supports http://en.wikipedia.org/wiki/Privilege_separation - although I've not had to use it for that.
I think you are looking for a sandboxed python. There used to be an effort to implement this, but it has been abolished a couple of years ago.
Sandboxed python in the python wiki offers a nice overview of possible options for your usecase.
The most rigourous (but probably the slowest) way is to run Python on a bare OS in an emulator.
Depending on the OS you use, there are several ways of running programs with restrictions, but without the overhead of an emulator:
FreeBSD has a nice integrated solution in the form of jails.
These grew out of the chroot system call.
Linux-VServer aims to do more or less the same on Linux.
I have written a program. I don't know if it is important how it is written but you can find it here: http://pastebin.com/Z3ZvVPV8 Basically, it asks you to assign values to variables and will perform calculations depending on what variables you chose, and prints the answer.
I would like to know how I can make the program run in a window other than cmd (I am using Windows Vista 32bit). I don't need much at all in terms of GUI, just a window that is a bit more user friendly/easier to look at when they are using the program.
EDIT: To those suggesting using IDLE, while that would work for me, if others want to use the program they would have to download it, so I was hoping for a way for that not to happen.
Python comes with a sort of default GUI package TkInter you can use it.
Also there is a lot of other GUI packages available.
The Python standard library offers a lot of ways to implemt simple (but also rather complex) GUIs. I'd like to point you at the documentation of TK (tool kit for graphical interfaces) http://docs.python.org/library/tk.html where you will find also some useful example of use.
Py2Exe is a viable option if you really don't need a gui. This will make it run and look like a command prompt, but it will be an .exe file. Here is a quick quote from thier page: "py2exe is a Python Distutils extension which converts Python scripts into executable Windows programs, able to run without requiring a Python installation."
Another alternative is to get Portable Python. Here is a quote from thier webpage: "Portable Python is a Python® programming language preconfigured to run directly from any USB storage device, enabling you to have, at any time, a portable programming environment. Just download it, extract to your portable storage device or hard drive and in 10 minutes you are ready to create your next Python® application." After packaging the portable python and your .py or .pyc file then create a .bat file that runs the portable python "Python-Portable.exe" with the correct command line parameters for loading your script. Be sure to use relative paths in the batch file in case they are running it from a flash drive, or something other than the same location as you.
NOTE: This is really not a good way to do this as thier download page states: "Installed size: based on selected packages, between 49MB and 480MB". Also be sure to read the the current Python Software Foundation License, as that is what Portable Python is released under, and it may or may not be legal to package it in a closed source project. I haven't really looked at the license myself to be able to tell you. If you are releasing it as open source, then there would not be an issue though. As a quick side note, if you need that .bat file to be a .exe file then you can use a .bat to .exe converter battoexe.com is one. This is really going the long way about doing the whole thing, but it is an option.
Sources:
Working with Python on and off for 7 years now, a lot that using a portable version on a flash drive, and also dealing with Batch files much longer.
I am trying to create a script in Python that will collect data put in the clipboard by the user and preferably save it as a list or in a text file or string/array/variable to work with later on.
This should work on Linux all versions (I would assume Ubuntu), Mac OS all versions and Windows all versions. I am not sure if 32bit and 64bit systems have different ways of accessing the data at the clipboard, if they do I guess it would be safe to make this work for the 32bit versions only so people running the 64bit versions can fall back onto the 32bit version of the OS.
The tricky part, apart from this having to work on the mentioned OS, is that I would like the script to run as long as the user does not stop it and while it runs all the data copied into the clipboard by the user is being copied to a list or in a text file or string/array/variable.
Of course there is a time limit at which the user can input data into the clipboard so I was thinking of having a loop scanning the clipboard every second or every 500 milliseconds, check if the content has changed, and if it has, copy it, otherwise don't copy it.
Is there a unified way or module that does this on all different OS or would it be better to write separate scripts for this task for the various OS?
The thing is, this is part of a bigger project that I would like to make work on Linux, Mac and Windows, so having those three options covered and then use Python code that can be used across the mentioned OS for the rest of the script/project would be ideal. Am I asking too much in general from this script concerning it having to work on Linux, Mac and Windows?
The xerox library supports Linux, Mac OS X, and Windows.
Note that it's a very bad idea to perform any action in short (< a minute) intervals, because that makes modern processors wake up regularily. You may want to use the respective operating system's APIs to register a callback once the clipboard changes.
You're probably better off using a more advanced gui toolkit than Tk, but it's in the standard library, so it's available everywhere.
As a really simple example:
import Tkinter
root = Tkinter.Tk()
root.withdraw() # Hide the main window (optional)
text_in_clipboard = root.clipboard_get()
print text_in_clipboard
You could use a GUI toolkit such as Qt to get a portable clipboard API. That said it might be a little overkill to use a whole GUI toolkit just for this. (Unless, of course, you're also going to use it to make a GUI.)
That said, clipboard APIs dealing with plain text should be reasonably simple to make your own abstraction over.
For instance, on OS X you can use PyObjC (which is installed along with OS X) to get plain-text contents of a clipboard:
from AppKit import NSPasteboard
from LaunchServices import
pb = NSPasteboard.generalPasteboard()
text = pb.stringForType_(kUTTypeUTF8PlainText)
CPU architectures
A 32-bit native app on a 64-bit OS will be accessing the same clipboard as a 64-bit one. If you need to support both architectures of an OS, and aren't writing a driver, for Windows it's okay to ship a 32-bit binary; for Linux you'll likely have to do both versions; for OS X, it should be reasonably safe to ship a 64-bit version, all Macs since mid-2007 have had 64-bit CPUs and the OS support is there since Leopard. A Python script will, on Linux, probably be executed by a Python installation from the distribution package manager, whose bitness will match the system, so you don't necessarily need to worry about that.
Polling is NOT robust/reliable.
You cannot determine if data has changed (on windows anyway) without pasting it into a buffer for inspection. This requires opening the clipboard. If you do this in a loop, you're going to collide with other apps. i.e. the app where the user is copying another item onto the clipboard. This will explode with an "cannot open clipboard" or "out of memory" error. This approach will not work reliably/robustly. You need to use proper clipboard monitoring APIs in the various platforms.
I suspect it's also possible to copy clipboard in platform neutral manner using pythonnet + TextCopy library via following links:
http://pythonnet.github.io/
https://stackoverflow.com/a/51912933/2338477
(See similar problem here: Quick and easy file dialog in Python? )
But haven't tried this by myself, might need also to play around what kind of commands you run and from which console. Let me know if someone will try this, I'll update more details in here.