Python Forum

Full Version: Open parrallel problem
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
*** 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.
The printer is buffered, so try a printer.flush(). This doc says to follow it with os.fsync() https://python-reference.readthedocs.io/...flush.html which may or may not be necessary depending on your OS.
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 !