Home > Software design >  Is there a way to clear the printed results and then run a function on the same line?
Is there a way to clear the printed results and then run a function on the same line?

Time:04-25

I am kinda new to python and I want to print a message and then after that delete that message and run a function that displays a list. Is there a way to do that?

CodePudding user response:

To clear the Python interpreter, you can use os.system() and execute a CLI command to clear the console like that:

import system
os.system('cls' if os.name=='nt' else 'clear')

If you are on Windows, the command would be cls, on Linux it's clear

CodePudding user response:

You can use this code

import os
def func():
    print([1,2,3,4])
print("Hello")
os.system("cls")
func()

CodePudding user response:

If You are using a Windows shell, like CMD, it is possible but it is not the best way to do so. Eg:

import os
clear = lambda: os.system('cls')
print("Hello")
input()
clear()
print("World")

This code prints hello in the first line. Then on pressing enter, it clears the terminal and prints world. Though this won't work if you are trying to clear a specific line.

A better way would be to use tkinter.

from tkinter import *
tk = Tk()
label1 = Label(tk, text="Numeric parameters")
label1.pack()
def a():
    global label1
    label1.configure(text="Hello")
button1 = Button(tk, command=a, text="Click Me")
button1.pack()
tk.mainloop()

This creates a GUI with a Click Me button that changes the text in the label on click.

  • Related