python用turtle调整文字位置,如何使python文本变成python turtle中的按钮?

I want to make the word "CAT" into a button, so when it's clicked it says "CAT". Also, the button I want should be in the position that the word is right now when it's not a button. Any help provided is needed. Thank you

I have already tried the tkinter module, but the problem with that is it opens a separate window with the button. What I want is a button on the main screen.

import turtle

screen = turtle.Screen()

# this assures that the size of the screen will always be 400x400 ...

screen.setup(800,800)

turtle.ht()

turtle.penup()

turtle.goto (50, 200)

turtle.color("black")

turtle.write("CAT", move=False, align="center", font=("Times New Roman", 120, "bold"))

screen.bgpic("background.gif")

turtle.st()

turtle.forward(145)

turtle.left(90)

turtle.forward(10)

turtle.pendown()

turtle.forward(110)

turtle.left(90)

turtle.forward(287)

turtle.left(90)

turtle.forward(110)

turtle.left(90)

turtle.forward(287)

turtle.ht()

I expect the output to be a huge button (in black) at the top of the screen saying "CAT". When that button is pressed, I want it to say "CAT" out loud. Right now there is just text at the top saying "CAT". I want to replace that text with a button saying the same thing. If I use on screen click I want the click to be in specific coordinates. How would I do that.

Thank You!

解决方案

You could use turtle do draw rectangle which could look like button. And you can useonscreenclick(check_button)to run functioncheck_button` when you click screen. If you clicked in rectangle then it could run function which does something.

import turtle

def show_cat():

turtle.ht()

turtle.penup()

turtle.goto (15, 220)

turtle.color("black")

turtle.write("CAT", move=False, align="center", font=("Times New Roman", 120, "bold"))

def check_button(x, y):

if -300 < x < 300 and 200 < y < 400:

show_cat()

screen = turtle.Screen()

screen.setup(800,800)

turtle.penup()

turtle.goto(-300, 200)

turtle.pendown()

turtle.begin_fill()

turtle.fillcolor('red')

turtle.fd(600)

turtle.left(90)

turtle.fd(300)

turtle.left(90)

turtle.fd(600)

turtle.left(90)

turtle.fd(300)

turtle.left(90)

turtle.end_fill()

turtle.onscreenclick(check_button)

turtle.mainloop()

Or you can use tk.Button with canvas.master as its parent, and put it on canvas using create_window(x, y, window=widget)

import turtle

import tkinter as tk

def show_cat():

turtle.ht()

turtle.penup()

turtle.goto (15, 220)

turtle.color("black")

turtle.write("CAT", move=False, align="center", font=("Times New Roman", 120, "bold"))

screen = turtle.Screen()

screen.setup(800,800)

canvas = screen.getcanvas()

button = tk.Button(canvas.master, text="Click Me", command=show_cat)

canvas.create_window(0, 0, window=button)

#canvas.create_rectangle((100, 100, 700, 300))

turtle.mainloop()

The same way you can put other tkinter's widgets on canvas

EDIT: example with more widgets

import turtle

import tkinter as tk

def show_cat():

label = tk.Label(canvas.master, text="Cat", font=("Times New Roman", 120, "bold"))

canvas.create_window(0, -300, window=label)

canvas.create_text(0, 300, text="HELLO", fill="red", font=("Times New Roman", 120, "bold"))

#-------------------------------------------

screen = turtle.Screen()

screen.setup(800,800)

canvas = screen.getcanvas()

button = tk.Button(canvas.master, text="Click Me", command=show_cat)

canvas.create_window(0, 0, window=button)

turtle.mainloop()

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值