How print to printer with Popen and GUI tkinter

By : eM. Key
Source: Stackoverflow.com
Question!

I use code from this question for printing to thermal EPSON TM-T82 printer but when I modified it with tkinter and mainloop() python not direcly printout my data until I close my layout GUI. I want this script can print out my data without I close layout GUI.

This is my code:

import subprocess
from Tkinter import *

class tes_print:
   def __init__(self):
       self.printData()

   def printData(self):
       data = " MY TEXT "
       lpr = subprocess.Popen("/usr/bin/lpr", stdin=subprocess.PIPE, shell=True)
       lpr.stdin.write(data)

root = Tk()
app2 = tes_print()
root.mainloop()
By : eM. Key


Answers

You are experiencing buffering. The write will write to a buffer which is not actually passed on to lpr until it fills up or you explicitly flush it.

Running lpr.communicate() will flush it and allow lpr to run to completion.

lpr = subprocess.Popen(['lpr'], stdin=subprocess.PIPE)
stdout, stderr = lpr.communicate(input=data)
By : tripleee


Regardless of iOS 10, you should start your location updating only if the permission was granted, you should also check if the permission is already granted before requesting permissions:

-(void)startLocationManager{
    self.locationManager=[[CLLocationManager alloc]init];
    self.locationManager.desiredAccuracy=kCLLocationAccuracyBest;
    self.locationManager.delegate=self;
    // Check for current permissions
    [self checkLocationAuth:[CLLocationManager authorizationStatus]];
}

-(void)locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status{
    [self checkLocationAuth:status];
}


-(void)checkLocationAuth:(CLAuthorizationStatus)status{
    switch (status) {
        case kCLAuthorizationStatusAuthorizedWhenInUse:
        case kCLAuthorizationStatusAuthorizedAlways:
            [self.locationManager startUpdatingLocation];
            break;
            // did not ask for permission, ask now
        case kCLAuthorizationStatusNotDetermined:
            if ([self.locationManager respondsToSelector:@selector(requestWhenInUseAuthorization)]) {
                [self.locationManager requestWhenInUseAuthorization];
            } else { // iOS < 8? implicitly request permission
                [self.locationManager startUpdatingLocation];
            }
            break;
        // Also need to handle failures, etc
        default:
            break;
    }
}
By : iMoses


I think preg_replace() is the better solution here as it will allow you to remove anything except alphabetical letters (as you requested in your comments):

$numChars = strlen(preg_replace("/[^A-Za-z]/", '', $getexpression));
By : icecub


This video can help you solving your question :)
By: admin