[英]How to combine Turtle with tkinter
So I've got tkinter code that when ran, opens up a new window with a square root calculator, when I try to get turtle in the same window as the square root calculator, 2 windows pop up So I've got tkinter code that when ran, opens up a new window with a square root calculator, when I try to get turtle in the same window as the square root calculator, 2 windows pop up
import tkinter as tk
from turtle import Turtle, Screen
root = tk.Tk()
canvas1 = tk.Canvas(root, width=400, height=300, relief='raised')
canvas1.pack()
label1 = tk.Label(root, text='Square root calculator')
label1.config(font=('helvetica', 14))
canvas1.create_window(200, 25, window=label1)
label2 = tk.Label(root, text='Enter number:')
label2.config(font=('helvetica', 10))
canvas1.create_window(200, 100, window=label2)
entry1 = tk.Entry(root)
canvas1.create_window(200, 140, window=entry1)
def sqr():
x1 = entry1.get()
label3 = tk.Label(root, text='The Square root of ' + x1 + ' is:', font=('helvetica', 10))
canvas1.create_window(200, 210, window=label3)
label4 = tk.Label(root, text=float(x1) ** 0.5, font=('helvetica', 10, 'bold'))
canvas1.create_window(200, 230, window=label4)
button1 = tk.Button(text='Calculate square root', command=sqr, bg='brown', fg='white',
font=('helvetica', 9, 'bold'))
canvas1.create_window(200, 180, window=button1)
pamest = tk.Button(root, text="Quit", command=root.destroy)
pamest.pack(pady=20)
screen = Screen()
screen.setup(500,350)
screen.screensize(600,600)
turtle = Turtle()
turtle.up()
root.mainloop()
Start with the turtle documentation sections that explain the standalone vs. embedded use of turtle.从海龟文档部分开始,这些部分解释了海龟的独立与嵌入式使用。 You're trying to use turtle in an embedded situation but are using the standalone API.您正在尝试在嵌入式情况下使用海龟,但正在使用独立的 API。 We can implement what you're trying to do as follows:我们可以按如下方式实现您要执行的操作:
import tkinter as tk
from turtle import RawTurtle, TurtleScreen
def sqr():
x1 = entry1.get()
label3 = tk.Label(root, text='The Square root of ' + x1 + ' is:', font=('helvetica', 10))
canvas1.create_window(200, 210, window=label3)
label4 = tk.Label(root, text=float(x1) ** 0.5, font=('helvetica', 10, 'bold'))
canvas1.create_window(200, 230, window=label4)
root = tk.Tk()
canvas1 = tk.Canvas(root, width=400, height=600, relief='raised')
canvas1.pack()
tk.Button(root, text="Quit", command=root.destroy).pack(pady=20)
label1 = tk.Label(root, text='Square root calculator', font=('helvetica', 14))
canvas1.create_window(200, 25, window=label1)
label2 = tk.Label(root, text='Enter number:', font=('helvetica', 10))
canvas1.create_window(200, 100, window=label2)
entry1 = tk.Entry(root)
canvas1.create_window(200, 140, window=entry1)
button1 = tk.Button(text='Calculate square root', command=sqr, font=('helvetica', 9, 'bold'))
canvas1.create_window(200, 180, window=button1)
canvas2 = tk.Canvas(canvas1, width=400, height=300)
canvas1.create_window(203, 450, window=canvas2)
screen = TurtleScreen(canvas2)
screen.bgcolor('pink')
turtle = RawTurtle(screen)
turtle.circle(50)
screen.mainloop()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.