Python - Increase size of specific console output

Viewed 6521

Working on a program for school. Finished the required part, now I'm just doing stuff for fun.

Basically you have to write a program which randomly draws a card from a deck, and then prints out the face value and suit. Easy -- took me like 5 minutes.

I'm trying to add a visual aspect to the assignment, using Unicode characters. I found these, and have written a bit of code to select the correct Unicode character given the card and suit. The only issue is that when the character is printed out, it's tiny. It's maybe two millimeters tall on my screen, which is nowhere near big enough to see the face of the card.

I've been searching for hours for some way of increasing the size of the output, to no avail. I found this snippet of code in my searching, but it doesn't seem to do anything (I am on Windows 10 Pro, by the way).

import ctypes

LF_FACESIZE = 32
STD_OUTPUT_HANDLE = -11

class COORD(ctypes.Structure):
    _fields_ = [("X", ctypes.c_short), ("Y", ctypes.c_short)]

class CONSOLE_FONT_INFOEX(ctypes.Structure):
    _fields_ = [("cbSize", ctypes.c_ulong),
                ("nFont", ctypes.c_ulong),
                ("dwFontSize", COORD),
                ("FontFamily", ctypes.c_uint),
                ("FontWeight", ctypes.c_uint),
                ("FaceName", ctypes.c_wchar * LF_FACESIZE)]

font = CONSOLE_FONT_INFOEX()
font.cbSize = ctypes.sizeof(CONSOLE_FONT_INFOEX)
font.nFont = 12
font.dwFontSize.X = 11
font.dwFontSize.Y = 18
font.FontFamily = 54
font.FontWeight = 400
font.FaceName = "Lucida Console"

handle = ctypes.windll.kernel32.GetStdHandle(STD_OUTPUT_HANDLE)
ctypes.windll.kernel32.SetCurrentConsoleFontEx(
        handle, ctypes.c_long(False), ctypes.pointer(font))

I was wondering if anyone had any insight or ideas on how I could go about making the characters larger. I've tried using a Turtle to draw the characters, but unfortunately it doesn't support the characters I want to use.

2 Answers

This turned out to be trickier than I thought. While most Python graphics interfaces will support a simple Spade (BMP) Unicode character:

print("\u2660")

Getting the Six of Spades (SMP) Unicode character is trickier:

print("\N{PLAYING CARD SIX OF SPADES}")
print("\U0001F0A6")
print("\uD83C\uDCA6")  # surrogate

I've tried using a Turtle to draw the characters, but unfortunately it doesn't support the characters I want to use.

Tkinter, and packages built on it like turtle.py and Zelle's graphics.py, depends on what the installed version of the Tk library can do. On my system, they could handle Spade but not Six of Spades.

I didn't have any better luck with the Python mode of the Java-based Processing environment, though that may reflect my lack of experience with it.

One environment, that I also don't have experience with, PyQt5, seems up to the challenge. This is my rework of a "Hello World!" program to display the card:

from PyQt5.QtCore import QSize, Qt
from PyQt5.QtWidgets import QApplication, QMainWindow, QLabel, QGridLayout, QWidget
from PyQt5.QtGui import QFont

class MainWindow(QMainWindow):
    def __init__(self):
        QMainWindow.__init__(self)

        self.setMinimumSize(QSize(640, 480))
        self.setWindowTitle("Playing Card Six of Spades")

        centralWidget = QWidget(self)
        self.setCentralWidget(centralWidget)

        gridLayout = QGridLayout(centralWidget)
        centralWidget.setLayout(gridLayout)

        font = QFont()
        font.setFamily("Apple Symbols")
        font.setPointSize(192)

        title = QLabel("\N{PLAYING CARD SIX OF SPADES}", self)
        #title = QLabel("\U0001F0A6", self)
        #title = QLabel("\uD83C\uDCA6", self)
        title.setFont(font)
        title.setAlignment(Qt.AlignCenter)

        gridLayout.addWidget(title, 0, 0)

if __name__ == "__main__":
    import sys

    app = QApplication(sys.argv)
    mainWin = MainWindow()
    mainWin.show()
    sys.exit(app.exec_())

enter image description here

Part of the challenge is that although Python running under Terminal.app prints the character:

enter image description here

The font that Terminal.app uses (Monaco) doesn't have the glyph for the card, the system switches to the Apple Symbols font to display the card. It took some experimenting to figure that out.

I know you're using a Windows-based Python, so not all of the above is applicable to your situation, but my suggestion is look into PyQt5.

There's no way to control size of specific character in a console/terminal session, be it windows or linux.

You can change all session font size, but not for individual character/words/lines like you may expect in a word processor.

The used font may have fixed or variable character width, but you can't control individual character size.

Related