I want my program to have a looping display, which the code works, but the problem is that the character display is a long continuous line whereas I want it to go back to the start of the printing after a certain amount of characters have been displayed.
This is the entire code at this current moment:
import time
import sys
def Startup(s):
for c in s:
sys.stdout.write( '%s' % c )
sys.stdout.flush()
time.sleep(0.25)
def StartUpDot(s):
while True:
timeout = time.time() + 3*5
Time = 0
for c in s:
sys.stdout.write( '%s' % c)
sys.stdout.flush()
time.sleep(0.25)
if Time == 5 or time.time() > timeout:
break
Time = Time - 1
Startup("BOOTING UP"),
StartUpDot(".......") #This is what i want to repeat
The function that I want to do what I ask is the StartUpDot function. It instead displays a continuous line like "BOOTING UP................. etc"
To clarify I want the ...... to repeat from the start, the "BOOTING UP" is from a different function, i put the comma there to make it one line. I have it in a loop if that helps.
Sorry about the confusion
You have two options:
If you want the text to print out on the line below the current text, you need to print a newline character to stdout
sys.stdout.write('\n')
If you're using a maximum line width of 10 characters your output will look like this:
1 BOOTING UP
2 ........
If you want to clear the current text and print on the same line, you need to print a carriage return to stdout, which returns the cursor to the front of the line. Then print empty space characters for the entire width of your line (to clear the old text), then another carriage return to return the cursor to the beginning of the line.
line_width = 10
sys.stdout.write('\r{0}\r'.format(' ' * line_width))
Using this method, the output would look like this (the line number is the same each time, so this is more like a time sequence).
1 BOOTING UP
1 OOTING UP.
1 OTING UP..
Here is an example
line_width = 10
for i in range(20):
msg = 'BOOTING{0}'.format('.' * i)
# Get only the last portion of the message
# that is shorter than the line width
msg = msg[-line_width:]
# Pad with spaces if shorter than line width
# Ensures old content is overwritten
msg = msg.ljust(line_width)
sys.stdout.write('\r{0}'.format(msg))
Related
I want to print two labels that have the same numbers on them. I am using ZPL. I have already made my print format in ZPL and it works properly. I am trying to print a data range. For example:
"What is the first number in the range?" User inputs 100
"What is the second number in the range?" User inputs 120
I would then get 40 labels in order.
I then want it to export that data into a notepad file and then print it to my default printer. My problem is that to print with ZPL I have to "tag" my data range with my ZPL code. I cant figure out how to get my data range to go into my print statement correctly. Please help. Thank you in advance!
import os
import sys
start = int(input("Enter the start of range: "))
end = int(input("Enter the end of range: "))
with open('TestFile.txt', 'a') as sys.stdout:
print('^XA')
print('^PQ2')
for labelRange in range(start, end + 1):
print('^FO185,50^A0,300^FD')(labelRange, end = " ")('^FS')
#print('\n')
print('^XZ')
os.startfile("C:/Users/joe.smith/desktop/TestFile.txt", "print")
exit()
here is something to get you started, but I doubt it is complete. You will need to provide a valid ZPL file for making the changes.
I also made the program use fixed numbers for now and so it just runs and outputs.You can change it back once you have it working.
start = 110
end = 111
notepad = ''
# these are header lines that go once (if windows you might need \r\n instead of \n)
notepad += '^XA\n'
notepad += '^PQ2\n'
for label in range(start, end + 1):
# use f-strings
notepad += f'^FO185,50^A0,300^FD{label}^FS\n'
# if you need some of those other numbers to increment
# then setup a counter and do the math here inside the f-string
notepad += f'^FO185,50^A0,300^FD{label}^FS\n'
notepad += '^XZ\n'
# with open('tf.txt', 'w') as sys.stdout:
# print(notepad)
print(notepad)
exit()
outputs:
^XA
^PQ2
^FO185,50^A0,300^FD110^FS
^FO185,50^A0,300^FD110^FS
^FO185,50^A0,300^FD111^FS
^FO185,50^A0,300^FD111^FS
^XZ
I'm trying to making a program where each keypress prints the next character in a predetermined string, so it's like the user is typing text.
Here's the code I'm trying to use:
def typing(x):
letter = 0
for i in range(0, len(x)):
getch.getch()
print(x[letter], end = "")
letter += 1
typing("String")
What happens here is you need to press 6 keys (The length of the string) and then it prints all at once. I can sort of fix this by removing the , end = "", which makes the letters appear one at a time, but then the outcome looks like this:
S
t
r
i
n
g
Any ideas for making the letters appear one at a time and stay on the same line?
You can try this code which works for me:
import time
def typewrite(word: str):
for i in word:
time.sleep(0.1)
print(i, end="", flush = True)
typewrite("Hello World")
This question already has answers here:
Print in one line dynamically [duplicate]
(22 answers)
Closed 9 years ago.
The community reviewed whether to reopen this question 3 months ago and left it closed:
Original close reason(s) were not resolved
I was wondering if it was possible to remove items you have printed in Python - not from the Python GUI, but from the command prompt.
e.g.
a = 0
for x in range (0,3):
a = a + 1
b = ("Loading" + "." * a)
print (a)
so it prints
>>>Loading
>>>Loading.
>>>Loading..
>>>Loading...
But, my problem is I want this all on one line, and for it it remove it self when something else comes along. So instead of printing "Loading", "Loading.", "Loading... I want it to print "Loading.", then it removes what is on the line and replaces it with "Loading.." and then removes "Loading.." and replaces it (on the same line) with "Loading...". It's kind of hard to describe.
p.s I have tried to use the Backspace character but it doesn't seem to work ("\b")
Just use CR to go to beginning of the line.
import time
for x in range (0,5):
b = "Loading" + "." * x
print (b, end="\r")
time.sleep(1)
One way is to use ANSI escape sequences:
import sys
import time
for i in range(10):
print("Loading" + "." * i)
sys.stdout.write("\033[F") # Cursor up one line
time.sleep(1)
Also sometimes useful (for example if you print something shorter than before):
sys.stdout.write("\033[K") # Clear to the end of line
import sys
import time
a = 0
for x in range (0,3):
a = a + 1
b = ("Loading" + "." * a)
# \r prints a carriage return first, so `b` is printed on top of the previous line.
sys.stdout.write('\r'+b)
time.sleep(0.5)
print (a)
Note that you might have to run sys.stdout.flush() right after sys.stdout.write('\r'+b) depending on which console you are doing the printing to have the results printed when requested without any buffering.
Trying to figure out how to stylistically print "Done." at the end of the window.
I am printing dynamically using flush. A snippet of code would be:
print("Opening the file "),
sys.stdout.flush()
for i in range(3):
print("."),
sys.stdout.flush()
print("\tDone.")
Except I would like "Done." to be printed all the way at the end of the line no matter how big the window is.
This finds the length of the console window, then makes a string of spaces, with Done. at the end.
import os
rows, columns = os.popen('stty size', 'r').read().split()
spaces = ''.join([' '] * (int(columns) - 5))
done = spaces + 'Done.'
print(done)
I would like to show one progress bar the resets when its finished. Here is the code. When you run it you can see it makes a new line every time .
import time
import sys
toolbar_width = 40
numbers = [1,2,4,5,6,7,8,9,10]
for number in numbers:
for i in xrange(toolbar_width):
time.sleep(0.1) # do real work here
# update the bar
sys.stdout.write("-")
sys.stdout.flush()
sys.stdout.write("\n")
Print a carriage return when you are done:
import time
import sys
toolbar_width = 40
numbers = [1,2,4,5,6,7,8,9,10]
for number in numbers:
#print number
for i in xrange(toolbar_width):
time.sleep(0.1) # do real work here
# update the bar
sys.stdout.write("-")
sys.stdout.flush()
sys.stdout.write("\r")
# ^^^^^^^^^^^^^^^^^^^^ <--- here!
This way, the cursor will go back to the beginning of the line.
However, this will go to the beginning of the line and keep what was there before, as #Kevin comments below. To blank the line you can print a long line with spaces surrounded by \r: the first to start printing at the beginning of the given line and the second to put the cursor back in the beginning.
sys.stdout.write("\r \r")
sys.stdout.write("\r" + " " * toolbar_width + "\r")
instead of
sys.stdout.write("\n")
Explain:
\r -- return at the beginning of line
" " * toolbar_width -- fill the line with space of the size toolbar_width
\r -- return again at the beginning of line