How to change Python background to a certain colour with colorama? - python-3.x

I am using colorama to change the colour of my text and it works fines, but now I want to change the colour of the whole background instead of just my text.
I am using Windows:
import colorama
from colorama import Fore, Back, Style
colorama.init()
print(Back.GREEN)
print(Fore.RED)
But, that code only makes the text coloured. Is there a way to do that in python? I want it like CMD where you can have the background of it a colour. I cannot use the OS module as I do not have admin rights, but I'm open to using any other module.
How do I solve this problem?

After a while in playing with it i figured it out. Just forgot about this post. Here is what I did.
import colorama
from colorama import Back as bg
colorama.init()
print(bg.RED)
print(colorama.ansi.clear_screen())
I think clearing the screen fixed the Issue

After playing with colorama on my Windows 10 box it seems it's only used to change the text but not the console/terminal background. I was however able to change the background using this standard library solution:
import ctypes
try:
ctypes.windll.msvcrt.system(b"color 4f")
print("Hello World!")
except ValueError:
pass
The terminal background will change to red with white text.

Related

Printing colored texts in Powershell, using python

I used termcolor to print a colored text which ended up printing a weird code in Powershell, thus I used colorama.init() but it didn't change anything. I copied and texted some other code from Stackoverflow as I failed to import colorama at the beginning.
import sys
from termcolor import colored
if sys.platform == 'win32':
try:
import colorama
except ImportError:
pass
else:
colorama.init()
text = colored("HI THERE!", color="magenta", on_color="on_cyan", attrs=["blink"])
print(text)
This codes prints out a colored message in Powershell (it doesn't blink though). However I don't know why this shorter code doesn't work. (It prints "[5m[46m[35mHI THERE![0m")
from termcolor import colored
import colorama
colorama.init()
text = colored("HI THERE!", color="magenta", on_color="on_cyan", attrs=["blink"])
print(text)
I do import termcolor and colorama; and use init, colored methods in both these codes but I don't know why it only works with the first code. Do you know what might be the reason behind this? Thanks in advance.

How do you create a perfect pygame fullscreen?

I am making a game, and I want it to be fullscreen. However, the pygame fullscreen is strange, creating a screen too large. So I referred to this: Pygame FULLSCREEN Display Flag Creates A Game Screen That Is Too Large For The Screen. However, when I followed these instructions
import ctypes
ctypes.windll.user32.SetProcessDPIAware()
true_res = (ctypes.windll.user32.GetSystemMetrics(0), ctypes.windll.user32.GetSystemMetrics(1))
pygame.display.set_mode(true_res,pygame.FULLSCREEN)
from an answer (but instead using pywin32 instead of ctypes, like this: win32api.GetSystemMetric(0)).
I used this, and while it does create a fullscreen, it also creates a black border around my screen and enlarges everything a slight bit, including my cursor. How can I get rid of this black border and get all shapes to normal size? Or is there a better way to create a good fullscreen?
If it helps, I use Windows 10.
Thanks in advance!
I think the problem of enlarging everything arose with the use of ctypes module as because the ctypes module makes use of a function named as GetSystemMetrics() whose work is to get the size of the screen of your system.
And might be the import pygame is loading some dll that is not compatible with a dll that windll needs.
So I suggest either you update the ctype library or pygame library or update both libraries or you can enlarge screen size by providing custom width and height values according to the resolution supported by your system.
Hope this helps !!

Can you print text in colors other than blue using a print command in python 3.2?

My code currently only prints text in blue but I would like some important messages to be printed using the print() command but in colors such as red.
I tried using something called colorama which I found in a different question's answer.
import colorama
from colorama import Fore, Back, Style
colorama.init()
print(Fore.RED+'Hello')
But it came up with this:
Traceback (most recent call last):
File "E:/Color test", line 1, in <module>
import colorama
ImportError: No module named colorama
Does anyone know how to do it or at least know if it is possible?
EDIT: I have found a similar question and tried all of the answers but none of them worked. I will really appreciate anybody's help.
You can do this using ANSI escape codes.
Black: \u001b[30m
Red: \u001b[31m
Green: \u001b[32m
Yellow: \u001b[33m
Blue: \u001b[34m
Magenta: \u001b[35m
Cyan: \u001b[36m
White: \u001b[37m
Reset: \u001b[0m
Suppose you want to print "Hello world" in blue, You will have to do:
print("\u001b[34m" + "Hello world" + "\u001b[0m")
Please note that I have added the reset code at the end to avoid coloring the rest of the text.
Additionally, if you are using a windows operating system you will need to clear the screen for the colors to appear, like this:
import os
os.system("cls")
Good luck!
There is a way - but that it is much more difficult than what I can just simply put in here and is a lot more advanced. is it possible to add colors to python output? Go to that weblink for more info.

Background color of tkinter label will not change (python 3.4)

I am making a widget with Tkinter in python 3.4. For some reason, I cannot change a label's background color from the default grey. The code for the label is something like this:
self.label = ttk.Label(master, text="Label Text",
foreground="blue", background="yellow")
Everything else works fine. I can change the foreground (text) color, however the background will not change, whether I am using label.config(), label['background'], or whatever.
I can change the background if I write it for Python 2.7, but I am using tutorials for Tkinter in 3.4, so this is undesirable.
This bug is caused by the 'aqua' ttk style on Mac OSX. It also breaks 'ttk.Progressbar' when set to 'indeterminate' mode. To fix both issues insert the following code after 'root = Tk()' to change the style ...
style = ttk.Style()
style.theme_use('classic') # Any style other than aqua.
This solution was posted by dietrich41
here : http://www.python-forum.org/viewtopic.php?f=12&t=16212
I tested it on a Mac running Python 3.4.1.

getting the color of pixels on the screen in Python3.x

I want to get the color of pixels on the screen in Python3.x. (example x,y)
I work on windows. and using tkinter.
But, It can't use PIL in Python 3.x.
How can i do this.
Thank you.
You can use PIL on Python3.x; there is a binary installer for Windows. The code might not work as is both on Python2.x and Python3.x:
try: import Image # Python 2.x
except ImportError:
from PIL import Image # Python 3.x
You could capture an area of the screen using an analog of grab() method.

Resources