1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Why is the print job not executed until the urwid application has finished?

Discussion in 'Main Forum' started by AFoeee, Sep 12, 2018.

  1. AFoeee

    AFoeee Guest

    I have written an urwid application which should print something when a button is pressed.

    However, after pressing the button, a print job is added, but it is not executed till the application is finished.

    About the execution environment:

    • os: ubuntu 18.04 (did also occur on raspbian)
    • python: 3.6.5
    • printer: a network printer (using CUPS)

    Could this be caused by a peculiarity of Linux or CUPS?

    I've also posted this question on stackoverflow, but I'm not sure if programming is really the problem.

    #! /usr/bin/env python3
    # -*- coding: utf-8 -*-

    import subprocess
    import urwid # installed via pip


    class App(object):
    PALETTE = [ ("revealFocus", "black", "dark cyan", "standout") ]

    def __init__(self):
    print_btn = urwid.AttrMap(urwid.Button("Print", on_press=self.print_dialog),
    "",
    "revealFocus")

    exit_btn = urwid.AttrMap(urwid.Button("Exit", on_press=self.exit),
    "",
    "revealFocus")

    pile = urwid.Pile([
    print_btn,
    exit_btn
    ])

    self.widget = urwid.Filler(pile,
    "top")

    self.loop = urwid.MainLoop(self.widget,
    App.PALETTE)

    def print_dialog(self, btn):
    output = "Username: testuser\n".encode() \
    + "Password: p4ssw0rd\n".encode()

    # why is this not executed immediately?
    lpr = subprocess.Popen("/usr/bin/lpr", # on raspbian: /usr/bin/lp
    stdin=subprocess.PIPE,
    stdout=subprocess.DEVNULL, # proposed by stackoverflow user elias
    close_fds=True) # proposed by stackoverflow user elias
    lpr.stdin.write(output)

    def start(self):
    self.loop.run()

    def exit(self, btn):
    raise urwid.ExitMainLoop()


    if __name__ == "__main__":
    app = App()
    app.start()

    Login To add answer/comment
     

Share This Page