简体   繁体   English

如何在python turtle.write()方法中颠倒文本?

[英]how to upside down a text in python turtle.write() method?

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. 如今,我正在编写一个程序,以从52张扑克牌中随机抽取4张扑克牌,我必须通过python turtle模块绘制这些扑克。 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! 我想使用do.settheading()设置数字的角度,但是我发现它不起作用! I can get a 5 but I can't get a upside-down 5 using the do.write() method...... 我可以用do.write()方法得到5,但是不能倒置5。

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? 代码来“绘制”一个数字,并通过设置起始角度,我可以“绘制”一个向上的数字2,但这会带来很多麻烦,不是吗?

Could anybody tells me how to write() a upside-down number? 有人可以告诉我如何写一个颠倒的数字吗? Thank you very much!!! 非常感谢你!!!

turtle doesn't have function to display text upside down. turtle没有倒置显示文字的功能。

But turtle is built on top of tkinter module and Canvas widget which has method 但是turtle是在tkinter模块和具有方法的Canvas小部件之上构建的

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 稍后您可以使用item_id更改角度

c.itemconfig(item_id, angle=45)

Effbot.org: Canvas in tkinter. Effbot.org:tkinter中的画布


BTW: I found information that only the newest tkinter with Tk 8.6 has angle= . 顺便说一句:我发现只有Tk 8.6的最新tkinter具有angle=
You can check version 您可以检查版本

import tkinter 

print(tkinter.TkVersion)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM