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.
Related
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.
I know these are questions: this and this and this. I have tried all of these methods in pycharm, but they either a) not work or b) I get a weird character on my screen, that looks like a 0 with diagonal line going through it. I can't copy it for some reason, so I can't show you.
For instance this:
import os
from time import sleep
print("lots of text")
sleep(5)
os.system('cls')
What should happen in the above is tht the message should be displayed for 5 seconds then disappear, but all I get is that weird character... In contrast when I ran my python program from command line, it worked as expected i.e. the screen cleared. I am very confused.
So my questions are:
Why don't any of them work in pycharm?
What does work in pycharm?
Ok, so, I have been having this problem as well — after scouring the internet, the os commands work BUT you need to enable the terminal emulator.
In the PyCharm toolbar, click Run, Edit Configurations, and the toggle for "Emulate Terminal in Output Console" should be under Execution in the pop up. Emulate Terminal toggle
I had to get help from the PyCharm team on this one, so here's to them responding at lightning speed.
Answer
The cls command doesn't work on a standard pycharm project because it is an IDE and certain os commands don't work in the same way it would with the windows command line.
There is currently no way to clear the console output in PyCharm, other than to manually do it by using the cls command through the terminal in PyCharm or through a keyboard shortcut set through the pycharm preferences menu. You can try to emulate a keyboard shortcut, but this would be difficult, and unless you absolutely need it, I would not recommend clearing the terminal output after a program is run.
References and Additional Links
https://teamtreehouse.com/community/clear-screen-for-pycharm-as-if-it-were-on-console-or-cmd
I don't know in which version of PyCharm this was introduced. I guess it must have been after this question was asked.
The way to clear the PyCharm Python Console is by using the context menu. Right mouse click in the upper part of the console (where the lines with >>> prompts are closer together) and select Clear All.
Alse see this PyCharm Support Community article.
Incidentally, none of the questions linked to in the OP's original post relate to PyCharm and do not answer the OP's question.
cls only temporarily clears it. Next command brings back all previous messages. The best option is to right click on the Terminal and choose Clear Buffer
I am beginner in wxPython, and I have two questions:
How can I put the Linux terminal in wxPython?
How do I connect the event, for example, of a combobox (or other) in Tabs (class1,class2,etc) to write text in TextControl (in class2)?
"The linux terminal" is a text console that needs to talk to a framebuffer or other display driver, so you're not going to be able to put that in a window without a whole lot of work.
But hopefully what you actually want is to just put any decent terminal in there—something you can run a shell in, ideally something that will match some TERMCAP entry so it can do things like curses, etc. You could build something like that yourself, but it's a lot of work. An easier solution is to use one of the pre-existing libraries for this, like wxTerm.
IDLE is being very dodgy as to when it will actually show an Auto-complete menu. As of late it hasn't been working at all, or, more specifically, only works during an interactive session.
I've been using Code Blocks for C, and have gotten really used to the very nice auto-complete features, so it's a bit frustrating not having them -- especially while trying to learn a new frame work and what class is associated with which methods, etc, etc..
Is there an easy fix to get IDLE auto-complete working again? I'm using python version 2.7.
Is there perhaps a simple editor I should look into? I've tried Vim, which was a little too heavy for my simple needs, Ninja, which I couldn't get to work for anything, and Sublime text 2, which I couldn't get my wxpython stuff to play with. What would a nice option be? Anything similar to Code Blocks would be cool, although, I'd be perfectly happy with IDLE if it would consistently work!
REQUESTED CLARIFICATION:
OK, so it seems I may have some of my terminology backwards. By non-interactive, I mean, for instance, right clicking on a python file and selecting "edit with IDLE". This brings up what I guess could be described as a text editor. You can enter all you code here. Once ready, you then hit F5, or select Run, and it launches (what I've been referring to as) the interactive terminal. It's here that you can type in code, press return, and instantly have that code evaluated.
What my question is referring to it the former, the part of IDLE where you edit the code. Sometimes while typing, after a . it will display the available methods, or after an open parenthesis it'll give hints as to the values expected. but the thing is, sometimes it does these things, sometimes it doesn't.
The only thing i found so far is that if an editing session of IDLE is connected with python shell (called "interactive mode" in the question, i.e. after an attempt to run the edited script) then "non-interactive" IDLE can autocomplete based on values in interactive window. For example, if I type
a = [];
a.appen
and then hit < Tab > it will do nothing, but if I previously type
a = []
in corresponding python shell, IDLE will autocomplete correctly.
So my only suggestion is import same modules with same names in python shell window in order to make them "visible" for non-interactive IDLE editor.
I had the same problem with IDLE, because I want to learn Qt and therefore autocompletion is very useful.
As it says in the settings of IDLE, you can trigger the autocomplete with "Control + Space", e.g. after a "QtGui.". Then a menu opens where you can arrow-scroll through the entries.
using the 'IDLE Editor window', you need to save and execute your code first.
The application running, turn back to the Editor window to use the auto-completion.
In my case, I had to open Options menu -> Extensions tab on the editor and look to make sure AutoComplete and other relevant options were enabled. They actually were, but by just clicking on 'Apply' even though I dint have to change anything did the trick for me.
Sometimes it has to do with the time you have to wait to get a suggestions.
When you go to options > extensions > general at completions popup wait you can change it to about 500ms.
In my case it was 2000ms by default.
hi everyone :)
my problem is, up until now, i have exclusively used tabs to indent python, as i find it easier than spaces, but for no reason i know, python interactive prompt, the basic python.exe one, suddenly refuses to accept the tab button, all it does is flash the cursor. all i can think of is that my computer in suddenly treading the window like any other, using tab to cycle input things, in this case the single one. also, before now, i could use the up button to reach previously typed code, the if i submit that line with no changes, use the down button to access the line that came after it, but now up works, but as if i had changed the line, eg moves me back to the "bottom" of the list of inputs, so down doesn't work.... my question simply is: how do i get my good old tab and down button to work like i want them to again? :(
thanks xxx
If you are using Windows with the standard cmd.exe console (and it would have been helpful for you to have stated this up front) then you can use the TAB and arrow keys exactly as you desire.
I recently observed this behavior too, on Windows, using cmd.exe. It also happens with Console2 - an alternate shell I sometimes use.
Though I do always use spaces in normal code in an editor, I had been accustomed to using the Tab key to indent in short multi-line inputs in the interactive python.exe interpreter. Recently that stopped working - pressing the Tab key flashes the cursor and doesn't indent. Using spaces does work fine here, though it's not as convenient past a couple of indentations.
I suspect (but am not certain) that the cause was installation of pyreadline or rlcompleter - I had been messing with trying to get tab completion in an interpreter in an embedded application on Windows. Of course in your case another installation could have included those packages.