Different coloured print strings in python - IDLE - python

I'm using IDLE to just create a short, basic text adventure game. the methods I try work in other editors such as Repl.it or canopy, but I prefer the use of IDLE as I just like the simplicity of it, and that it doesn't require an Internet connection to access.
I won't mention the methods I tried, as I may have just been approaching them in the entirely wrong way, or maybe IDLE just simply doesn't have the functionality of other editors, but I just want to make the text in the game clearer to distinguish. For example, below is a short segment:
import time
print ("Welcome Traveller!")
time.sleep(1)
def name_identify():
print ()
name_input = input ("What is your name? ").lower()
name_input = name_input.title()
time.sleep(0.75)
def name_confirm():
print ()
print ("So %s is your name then?" % name_input)
Where the questions such as
name_input = input ("What is your name? ").lower()
appear in a different colour, or bold/italic even. As things similar to
print ("\x1B[3mHello World\x1B[23m")
and
print ("\033[1;31m""Hello world")
don't work, I assume this function is not naturally supported, so I ask, if it is possible to either A, make it supported, so this method, or similar work, or B, another option which does make changes at least similar to what I seek?
It would be very helpful if anyone could provide me with a way to do this, or at the very least an editor which is close in simplicity (design wise) to IDLE and does support the functions I seek. The version of python I am currently using is 3.6.1.
Thanks in advance, and I apologise if my wording confuses you, just ask about what confused you and I will attempt to clarify. Using Windows with the Idle Shell.
Due to the limitations of some python environments, I just used Tkinter to create an interactive procedurally generated script which you can scroll along as if it were the shell, with the added bonus of adding pictures for more depth.

I know what you are going through right now, but there is no benefit to make text colored or bold in python without GUI. If somehow you manage to do it it will turn back to normal text when you make the file to exe format. I have wasted a week on this.
It only makes sense when you are reach GUI programming. Tkinter would be a good place to start.
Its does not matter on the IDE but on the compiler.
In tkinter its a matter of seconds
lab_practice = label(text = "practice",fg = "red#the foreground color",bg = "yellow",font = ("Arial",40))

Related

How to disable or ignore python input in the console? on Mac

I see a lot of answers to this question online, but the only solution I have found uses the msvcrt module that (as I understand it) is only available for Windows.
I am making a simple python console game and I want to stop the user from typing anything while the application is loading or playing a simple animation as it tends to break the program.
Example:
import time
#disable input here
print('hi')
time.sleep(3)
print('3 seconds have gone by')
#enable input
I don't want to let the user roll their face over the keyboard and make everything messy and ugly. I know mac is very strict about this kind of stuff, is it even possible?
From the solution you linked I can see that you are only needing the msvcrt.getwch() function, so you could use the one from the getch module i.e. getch.getch().

How do I clear text on shell?

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.

Text Based Game, Drawing Unicode Python

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()

Python Multithreading to allow user to exit at anytime

I'm writing a text based RPG, and I am using Python 3.3.4. It will be played through the Python command line, with no graphics. I'm wanting to make it so that no matter what options the user is presented with, they have the capability of typing "exit" or "help" and exiting (or getting help information) respectively at any time during the game. So if they're in the middle of fighting some monster and the only options presented to them directly would be to attack, defend, or flee, they're still able to exit or get help. Same for once they leave that function and they're just moving around the map, or talking to NPCs. From what I've found, starting a thread that waits for "exit" to be entered is the best way to do that. If I'm wrong, please let me know! If not, please explain (or show me a guide) how I'm supposed to go about this because none of my ideas have worked.
I've attempted using the threading module, and _thread. I may not have implemented them correctly, and I have no code to show for my attempts as I just trashed it when it didn't work.
Thank you in advance.
Your game doesn't have to do any work in the background, so you do not need to spawn any additional threads. The main loop of your program will listen for user input, perform the action, and then prompt the user for their next action. It should look something like this:
while True:
input = get_user_input()
if input == 'exit':
break
next_actions = modify_game_state(input)
print "You can now do: %s" % next_actions
Edit: You should not have more than one thread accept user input. It's possible for one of the threads to gobble information that was intended for the other, so no matter what solution you decide on, don't add a thread that just checks if the user input was "exit", unless it reads all of the user input.
Looking back at this, I feel like an absolute idiot.
The proper way to handle this would be calling a function that accepts input and checks it against different expected strings or numerical values.
I just thought I'd answer this in a little more clear way than Stu did.

Printing over previous prints in python?

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.

Categories

Resources