I need to clear the IDLE shell, using code. The only way I know of to remove the text is closing the shell and reopening. I want this to be able to put into code that requires refreshing the shell, for example a memory game, giving a string of words, and then removing them, or some kind of animation made of text pictures.
Essentially I want to do something like shell.clear() or something similarly easy to use. It can be a function or whatever, but I'd like it to be easy to put into some preexisting code.
I do not know if such a thing is possible, but if you have any pointers, tips or code, I'd appreciate the help.
IDLE 3.7.3 on Mac.
You can use:
from os import system
#for windows
system('cls')
#for Unix based systems
system('clear')
Or if you are inside IDLE or Python interpreter you can use this function:
def cls(): print ("\n" * 100)
That you can call cls() whenever you need to clear your screen.
Related
I'm a starting programmer looking to make a simple text based RPG from scratch. I know there might be an easy tool to do this but I want as little handed to me as possible to use this project as a sort of learning possible. I've been using Python and so far I really like it (I'm willing to use Java or Javascript if absolutely necessary.)
My problem though is that right now I'm using the console to run the game but I'd prefer to run it as a standalone application (also so I can distribute it in like an .exe or similar). Is there some simple way I can do this? Everything is in Unicode, so it just needs to be able to display Unicode text (in-line preferably) and have some way to check for key presses (to type commands).
I've looked into Kivy, but it seems far beyond what I need and the text it displays is not in-line and must be displayed line by line. Plus it doesn't seem to be able to be exported to a single file.
Thanks for the help and remember I'm very much a newbie.
If you want a GUI in Python, you can use TkInter, which is fairly easy to learn (https://wiki.python.org/moin/TkInter).
However, if you want to make it an executable so you can share it then you have to use something like the following:
cx_freeze (http://cx-freeze.sourceforge.net/)
py2exe(http://www.py2exe.org)
PyInstaller(http://www.pyinstaller.org)
These will 'freeze' your Python scripts by including the interpreter and libraries in the .exe file. There's a lot of information in this previously asked question; How do i convert a Python program to a runnable .exe Windows program?
Here's a basic example of a text thing in tkinter:
from tkinter import *
root = Tk()
playerEntry = Entry(root)
textLabel = Label(root, justify=LEFT)
playerEntry.pack()
textLabel.pack()
def changeText(addText):
textLabel.config(text = textLabel["text"] + addText + "\n")
def get(event):
changeText(">>> %s" % playerEntry.get())
do_stuff()
playerEntry.delete(0, END)
def do_stuff():
changeText("Stuff is happening")
playerEntry.bind("<Return>", get)
root.mainloop()
In python 2.7, how do I not only clear screen but make the prompt begin at the top of the screen. I am surprised that I was not able to find a quick answer for this online. It seems like one of the most basic things someone would desire to be able to do when working with the shell.
If this is a duplicate then by all means direct me to original because I can't find one.
OH yeah, I am on Windows 7.
Maybe you can try the following:
import os
os.system('cls')
If you use the IPython shell (which has many useful features), you can just type:
clear
It is best to just define a function like 'clear()' and use that to repeatedly clear the console.
from os import system
clear = lambda: system('cls') # For Windows
And now you can use 'clear()' whenever you want.
I am using DataNitro to write Python Script in Excel. Its very useful indeed. However, when I open the Idle editor in excel, the accompanying Python Shell is not interactive, in that it does not return print statements, show errors, nothing. It just restarts every time I run the programme. This makes it incredibly hard to debug as I can't use print statements to trace the errors.
Does anyone know if this is a bug with DataNitro, or is it supposed to be that way, or whats going on? are there any solutions?
Thanks so much
Our IDLE editor is just an editor - it doesn't work as a shell.
The best way to debug programs is to raise an exception. This will freeze the shell that opens when a script is run, and you'll be able to inspect the variables and see any print statements that were generated during execution.
For example, if you run:
print Cell("A1").value
x = Cell("B1").value
raise
You'll see the value of A1 printed to the shell, and you can enter "x" at the prompt to see the value of B1.
You can also import a script you're working on into the regular Python shell (the one that opens when you press "shell"). This will execute the code in that script.
We'll be adding a guide to debugging code to the site soon, as well as some features that make it easier.
Source: I'm one of the founders of DataNitro.
Not as knowledgeable as Ben, but have been using DataNitro quite a bit and here are some tips:
The shell automatically closes once the script has run. If you want to inspect some prints or even interact with the shell I normally place following at end of my script.
raw_input("Press Enter to Exit shell")
Not very elegant, but I have even created a small loop that displays text options in the console. Can then interact with your program and sheet from there. Clever and more elegant way would be to have your script poll an excel cell and then take action form there.
Something else that you might find nice is that it also also you to run Ipython instead of the default python shell. Cannot imagine using python without Ipython... so you get benefits of tab completion Ipython debugging etc. To activate that just click the "Use Ipython" Checkbox in DataNitro Settings (don't know if this is version dependent).
I'm doing a script with a menu-like beginning , and I wanted to know if I could do something like this:
You open the file and it prints this menu :
LOGO
Welcome to script 11 , what would you like to do?
-write a file
-read a file
-create a file
> #input here
You select write ( for example) and it prints this OVER the previous menu intead of printing if after it:
LOGO
Writing a file!
-Select the path:
> #input here
Thanks for the help in advance.
Edit: I didn't want to completely erase the screen( I've seen the other threads about that) but if there is a method to erase only some lines , but the anwers tell me that it isn't possible without external libraries so I'll just clean all the screen and reprint some things, but thanks for all the anwsers
If you are using Linux (or OSX), you can use the curses module.
If you are using windows, use the console module.
If you want to display a multi-line menu, and display an entirely new one after each menu choice, there are only a few ways to do this:
Clear the screen before printing each menu. There are about 69102 questions on SO about how to do this; how to clear the screen in python has links to many of them.
Print out a form feed/page feed. Which will not work on many modern terminals (Windows or Unix), but it rocks on old-school teletypes.
Use terminal control sequences to move the cursor around. This will work on Windows if the cmd terminal is ANSI-enabled (I believe it usually isn't by default), and on everything else if you pick (or look up) the right terminal to send control sequences for. You will also have to make sure to overwrite each character, not just each line. Otherwise, when you overwrite line 2 you'll end up with "Writing a file!pt 11 , what would you like to do?".
Use curses (and your favorite third-party Windows curses port), or some other terminal graphics library, to do this all at a higher level. (Writing separate code using console if it exists—for Windows—and curses otherwise—for almost everything else—is often the simplest way to do this.)
I'd suggest 1 or 4, but those are your options.
Can someone help me out please...I'm trying to start my first programming project. It will be implemented in python.
I need to have a textbox (which i am using wxpython for). If the user enters any text into this text box, then I want it to appear as arabic. I wanted to this by automagically changing the users Keyboard to an arabic layout when the cursor lands in the given text box.
So i found this pywin32 module, which has a function LoadKeyboardLayout()
So i am trying to test this in IDLE, to see if I can make it accept arabic text into IDLE, to see if it works. So I enter, into IDLE:
win32api.LoadKeyboardLayout('00000401',1)
This then returns, 67175425, the decimal equivalent of hex:'4010401' whcih I believe is the locale ID for Arabic. SO I think wow! I've done it, but when I try typing after this, in the IDLE window, it continues to type normal english characters.
Can someone please explain my errors and guide me towards a good solution.
UPDATE
Okay, I've been trying to solve this problem ever since posting the damn question.
No luck.
Then, I thought, "ok, screw it, instead of testing it quicly in IDLE, I will just try it out, in situ, in my source code for the project."
WTF - it worked first time, giving exact behaviour that I wanted.
Then I tried it in a different IDE, in the interpreted window, and again, IT WORKED straight away!
So clearly my issue is with IDLE, in its interpreting mode.
Can anyone explain why it doesn't work in the IDLE shell???
Keyboard layout setting in Windows is per-process (and inherited from the parent process)
IDLE runs your Python script in a background process separate from its GUI
So you have successfully changed the keyboard layout of the background Python process that is running your script, but not of IDLE's GUI.