我想将“ CAT”一词变成一个按钮,因此单击它时会说“ CAT”。另外,当我想要的按钮不是按钮时,它应该处于单词现在正确的位置。需要提供任何帮助。谢谢
我已经尝试过tkinter模块,但是问题在于它使用按钮打开了一个单独的窗口。我想要的是主屏幕上的按钮。
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()
我希望输出在屏幕顶部显示一个巨大的按钮(黑色),并显示“ CAT”。当按下该按钮时,我希望它大声说出“ CAT”。现在,顶部仅显示“ CAT”文本。我想用一个表示相同内容的按钮替换该文本。如果我在屏幕上单击,则希望单击位于特定的坐标中。我该怎么做。 谢谢!
答案 0 :(得分:0)
点击屏幕时,您可以使用turtle do draw rectangle which could look like button. And you can use
onscreenclick(check_button)to run function
check_button`。如果单击矩形,则它可以运行执行某些功能的函数。
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()
或者您可以将tk.Button
作为其父项使用canvas.master
,并使用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()
以相同的方式将其他tkinter的小部件放置在画布上
编辑:带有更多小部件的示例
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()
答案 1 :(得分:0)
我们可以利用turtle的面向对象特性来定义我们自己的可重用按钮类,以生成多个按钮:
from turtle import Screen, Turtle
class Button(Turtle):
FONT_NAME, FONT_SIZE, FONT_TYPE = 'Arial', 18, 'normal'
HORIZONTAL_PAD, VERTICAL_PAD = 1.05, 1.15
def __init__(self, text, position, command=None):
super().__init__(visible=False)
self.speed('fastest')
self.penup()
self.text = text
self.position = position
self.command = command
self.width, self.height = self.drawButton()
def drawButton(self):
x, y = self.position
self.setposition(x, y - self.FONT_SIZE/2)
button_font = (self.FONT_NAME, self.FONT_SIZE, self.FONT_TYPE)
self.write(self.text, align='center', move=True, font=button_font)
width = 2 * (self.xcor() - x) * self.HORIZONTAL_PAD
height = self.FONT_SIZE * self.VERTICAL_PAD
self.setposition(x - width/2, y - height/2)
self.pendown()
for _ in range(2):
self.forward(width)
self.left(90)
self.forward(height)
self.left(90)
self.penup()
return width, height
def clickButton(self, x, y):
c_x, c_y = self.position
half_w, half_h = self.width/2, self.height/2
if c_x - half_w < x < c_x + half_w and c_y - half_h < y < c_y + half_h:
(self.command)(x, y)
screen = Screen()
cyan = Button("Cyan Background", (100, 100), lambda x, y: screen.bgcolor('cyan'))
screen.onclick(cyan.clickButton, add=True)
yellow = Button("Yellow Background", (-100, -100), lambda x, y: screen.bgcolor('yellow'))
screen.onclick(yellow.clickButton, add=True)
screen.mainloop()
随心所欲地模样。