Python turtle works with tkinter. How to get the root you know from tkinter?
Like this:
import tkinter
root = tkinter.Tk()
but for turtle.
The top-level widget is available through the winfo_toplevel method of the turtle canvas:
import turtle
canvas = turtle.getcanvas()
root = canvas.winfo_toplevel()
It is of a subtype of Tk:
import tkinter
assert type(root) is turtle._Root
assert isinstance(root, tkinter.Tk)
As pointed out by #das-g
root = turtle.getcanvas().winfo_toplevel()
gives you an object representing the turtle root window.
However, if your use case is to integrate turtle graphics with a full-blown Tkinter application, the explicit approach should be preferred at all times:
from tkinter import *
import turtle
root = Tk()
turtle_canvas = turtle.Canvas(root)
turtle_canvas.pack(fill=BOTH, expand=True) # fill the entire window
protagonist = turtle.RawTurtle(turtle_canvas)
protagonist.fd(100) # etc.
This adds the extra benefit of being able to control position and size of the turtle canvas. Plus, having explicit code helps others understanding it.
turtle.getcanvas()
returns the object you are (I am) looking for.
Related
I have two scripts that both work:
import tkinter
root = tkinter.Tk()
root.configure(bg='blue')
root.mainloop()
and
from tkinter import *
root = Tk()
text = Text(root)
text.insert(INSERT, "Hello world!")
text.pack()
root.mainloop()
I want to combine the two scripts to print the text on a blue background, but moving anything from one script to another seems to break it.
I can't figure out if it's about root = tkinter.Tk() vs root = Tk(), or import tkinter vs from tkinter import *, or something entirely different. I can't find a successful combination.
I'm using Ubuntu and Python 3.6.9.
Because you use two different styles when importing tkinter, you will need to modify the code from one file when moving to the other. The code in your first example is the preferred way to do it because PEP8 discourages wildcard imports.
When when you copy the code from the second example, you'll need to add tkinter. to every tkinter command (tkinter.Tk(), tkinter.Text(root), tk.INSERT, etc.
Personally I find import tkinter as tk to be a slight improvement. I find tk.Tk() to be a little easier to type and read than tkinter.Tk().
You should know that:
from tkinter import *
will import all the attribute in the tkinter.But if you also define some variable in your script.It will be covered by your new variable.So we don't recommend you to use that.(If you used both from tkinter.ttk import * and from tkinter import *.Some default widgets of tkinter will be covered by ttk widgets.)
Just like Mr.Bryan said,I'd like to use import tkinter as tk,too.
I am making a program where the Turtle utilizes a Tkinter Canvas to draw on (RawTurtle(tkinter.canvas)). I want to change the shape of the turtle (with a .gif image).
I tried the functions with turtlescreen() provided with the documentation on this but tkinter.Canvas doesn't have a register_shape or addshape method.
Here's example code that does what you describe:
from tkinter import Tk, Canvas
from turtle import RawTurtle, TurtleScreen
root = Tk()
canvas = Canvas(root)
canvas.pack()
screen = TurtleScreen(canvas)
screen.register_shape("test.gif")
turtle = RawTurtle(screen)
turtle.shape("test.gif")
# body of code here ...
screen.mainloop()
Perhaps you got one of the steps out of order? Also, the turtle library provides ScrolledCanvas as an alternative to Canvas.
I'm learning Python by building an airplane ticket app. I'm currently working on learning TkInter for the GUI, and I want to know why I have to create a container (frame) instead of just putting everything in root = Tk() which seems to work.
Also, isn't ttk part of tkinter, therefore I shouldn't have to have the 2nd line (from tkinter import ttk)?
Here's my code:
from tkinter import *
from tkinter import ttk
root = Tk()
frame = Frame(root)
root.title("AirTix")
flight_title = Label(frame, text = "Flights").grid()
root.mainloop()
Thanks!
I want to know why I have to create a container (frame) instead of just putting everything in root = Tk() which seems to work.
You do not have to create a container. Whatever tutorial or book is telling you that you must is wrong. There's nothing wrong with creating everything in the root window.
That being said, for anything but the most trivial of apps, it helps tremendously to organize your widgets in logical groups, with each logical group being a frame (or, perhaps, a Canvas or PanedWindow). This gives you the flexibility to use the best geometry manager (pack, place, or grid) for each section.
For example, it makes sense to have a toolbar that is a frame with a bunch of buttons packed left-to-right. The same might be true for a bottom status bar. The main body might be a frame with widgets arranged in a grid.
grid can be a bit more complicated to use, and pack excels at placing widgets either in a single horizontal row (toolbar or status bar), or column (toolbar on top, status bar on bottom, main area in the middle.
Also, isn't ttk part of tkinter, therefore I shouldn't have to have the 2nd line (from tkinter import ttk)?
Even though ttk is part of tkinter, it does not get imported when you import everything from tkinter. Many python packages are this way, with sub-modules that must be explicitly imported.
Further, it's bad practice to use a wildcard import. Instead of doing this:
from tkinter import *
root = Tk()
... it's arguably better to do it like this:
import tkinter as tk
root = tk.Tk()
With the former, you end up polluting the global namespace with a bunch of things you may or may not use. With the latter, you import exactly one thing. Plus, it makes your code more self-documenting because it makes it crystal clear when you are expecting to use an object or class from the tk package.
I would like to display the app on a specific postion (regardless of the screen models).
I have at the moment
from Tkinter import *
root = Tk()
Thanks for answering and thanks for checking if you don't know either a way to do it or directly the answer for : Writable but transparent
To control placement of tk root window use geometry
from Tkinter import *
root = Tk()
root.geometry("+1120+110") # x and y coordinates on a screen
root.mainloop()
I am writing a program that requires the tkinter canvas to update graphics. In its previous form it was written in turtle. The turtle function to update the canvas is
turtle.ontimer()
Is there a tkinter equivalent?
You can use after method:
For example:
root = Tk()
...
root.after(2000, callback) # call `callback` function in 2000 ms.