0

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()
cdlane
42.2k5 gold badges37 silver badges87 bronze badges
asked Dec 15, 2021 at 18:32

1 Answer 1

1

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. 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()

enter image description here

answered Dec 15, 2021 at 19:59
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.