Python Forum
Open parrallel problem - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: GUI (https://python-forum.io/forum-10.html)
+--- Thread: Open parrallel problem (/thread-16232.html)



Open parrallel problem - Clement_2000 - Feb-19-2019

*** I Don't know why, I already tried to post this, but for some reason it seams that my post disappeared or was never posted (maybe I didn't press the right button). Anyway, if it got removed by a moderator because something was wrong, feel free to remove this one again, please just tell me what's wrong by PM. Thank you***

Hello,
I'm making a piece of software to control my printer by sending commands to it.
But now I got a big problem, which is that the printer wont do anything until I close the port.

Here is an example :
from tkinter import *

printer = open("/dev/lp0", "w")

win = Tk()
win.geometry("100x100")

def send(text):
    printer.write(str(text)) #Print text
    printer.write("\x0A\x0D") #Line feed and carriage return

textEntry = Entry(win)
textEntry.pack()

sendText = Button(win, text="Print", command=lambda:send(textEntry.get()))
sendText.pack()

win.mainloop()

printer.close()
When pressing any button, nothing happens until I close the window.
Any idea ?

Thank you in advance,
Clément.


RE: Open parrallel problem - woooee - Feb-19-2019

The printer is buffered, so try a printer.flush(). This doc says to follow it with os.fsync() https://python-reference.readthedocs.io/en/latest/docs/file/flush.html which may or may not be necessary depending on your OS.


RE: Open parrallel problem - Clement_2000 - Feb-19-2019

Oh yes, thank you !! Smile
It works with :
from tkinter import *

printer = open("/dev/lp0", "w")

win = Tk()
win.geometry("100x100")

def send(text):
    printer.write(str(text)) #Print text
    printer.write("\x0A\x0D") #Line feed and carriage return
    printer.flush()

textEntry = Entry(win)
textEntry.pack()

sendText = Button(win, text="Print", command=lambda:send(textEntry.get()))
sendText.pack()

win.mainloop()

printer.close()
THANK YOU SO MUCH !