3

nowadays I'm writing a program to fetch 4 poker cards from 52 poker cards randomly and I have to draw these pokers by python turtle module. Now here's my question: cause there's an upside-down number in pokers, just like this(the bottom right corner number)

at first I want to use this code to generate the numbers:

import turtle as do
def generate_digital(number, x, y, start_angle, size):
    '''
       this function generate '2-10'
       parameters:
       number: this is number you want to write
       x and y: this is the pen's initial location
       start_angle: the pen's initial direction
       size: the number's size
    '''
    do.penup()
    do.goto(x, y)
    do.pensize(30)
    do.setheading(start_angle)
    do.write(number, font=("Arial", size, "normal"))

I want to use do.settheading() to set the angle of the number, but I found that it didn't work! I can get a 5 but I can't get a upside-down 5 using the do.write() method......

Now, the only way myself can think of is to use this

def generate_photo_2(x, y, start_angle, size):
    '''
       this function generate a '2'
       parameters:
       just like last function
    '''
    do.penup()
    do.goto(x, y)
    do.pensize(3)
    do.setheading(start_angle)
    do.pendown()
    do.circle(-size, 200)
    do.fd(2 * size)
    do.left(45)
    do.fd(0.6 * size)
    do.left(90)
    do.fd(2 * size)

code to 'draw' a number, and by setting the start angle, I can 'draw' a upside-side number 2, but it causes a lot of trouble, isn't it?

Could anybody tells me how to write() a upside-down number? Thank you very much!!!

ggorlen
  • 44,755
  • 7
  • 76
  • 106
Fletcher Wang
  • 45
  • 1
  • 4
  • Related: [How to create rotated text in Python's turtle graphics?](https://stackoverflow.com/questions/72749874/how-to-create-rotated-text-in-pythons-turtle-graphics) and [how to rotate text in python's turtle graphics](https://stackoverflow.com/questions/4469125/how-to-rotate-text-in-pythons-turtle-graphics) – ggorlen Dec 06 '22 at 00:47

1 Answers1

2

turtle doesn't have function to display text upside down.

But turtle is built on top of tkinter module and Canvas widget which has method

create_text(x, y, text=.., angle=..., ...)

Working example

import turtle

c = turtle.getcanvas()

item_id = c.create_text(0, 0, text='5', angle=180, font=("Arial", 30, "normal"))

turtle.mainloop() # run tkinter event loop

Later you can change angle using item_id

c.itemconfig(item_id, angle=45)

Effbot.org: Canvas in tkinter.


BTW: I found information that only the newest tkinter with Tk 8.6 has angle=.
You can check version

import tkinter 

print(tkinter.TkVersion)
furas
  • 134,197
  • 12
  • 106
  • 148