Is it possible to use the turtle module to obtain coordinates (turtle.pos() once at the desired locations) without it opening a canvas?
I know this probably seems silly and defeats the point of the turtle module, but it's actually a very useful way of getting the coordinates of some points I need.
Many thanks
Instead of running turtle standalone, you could run it embedded and withdraw the root window on startup:
import tkinter as tk
from turtle import RawTurtle
root = tk.Tk()
root.withdraw()
canvas = tk.Canvas(width=500, height=500)
turtle = RawTurtle(canvas)
turtle.sety(-1)
turtle.circle(1, extent=45)
print(turtle.position())
Related
I've been happily going along writing programs using Python Turtle Graphics, and occasionally super-charging that module with forays into tkinter when turtle wasn't quite up to the job. An example is shown below.
What would be the most direct translation into "non-terrible" pure tkinter code? Is it always recommended to use OOP with tkinter? If so what would my program look like using that paradigm? If not, how would it look with just procedural tkinter? I've seen so many different ways to work with tkinter and create roots and masters etc., it's all a bit confusing. I'd like to keep the simplicity of the the approach I have used as much a possible. As usual, any help much appreciated.
import turtle
import tkinter as tk
def do_stuff(a_turtle):
for color in ["red", "yellow", "green"]:
a_turtle.color(color)
a_turtle.right(120)
def press():
print("ouch")
if __name__ == "__main__":
screen = turtle.Screen()
screen.bgcolor("cyan")
canvas = screen.getcanvas()
button = tk.Button(canvas.master, text="Press me", command=press)
canvas.create_window(-200, -200, window=button)
my_lovely_turtle = turtle.Turtle(shape="turtle")
do_stuff(my_lovely_turtle)
turtle.done()
Is it possible to make turtle.Screen() background transparent?
If not then how to draw lines on screen?
I want to create program which gives me hints on pool game.
Since turtle is built atop tkinter, if we work with an embedded turtle, rather than standalone, then your ability to create a transparent turtle window is a function of your version of the Tk library and your operating system platform:
import tkinter as tk
from turtle import RawTurtle, TurtleScreen, ScrolledCanvas
root = tk.Tk()
root.attributes("-alpha", 0.3)
canvas = ScrolledCanvas(root)
canvas.pack()
screen = TurtleScreen(canvas)
turtle = RawTurtle(screen)
turtle.width(10)
turtle.penup()
turtle.sety(-100)
turtle.pendown()
turtle.circle(100)
screen.mainloop()
In other words, "maybe".
The essence of this answer is the same as #cdlane's answer: obtain a reference to the Tk root window, and set its transparency with myroot.attributes('-alpha', 0.3). cdlane's answer obtains the rootwindow by starting from Tk, and constructing all the necessary Tk objects before constructing a TurtleScreen and Turtle. This answer starts from the other end: it allows turtle to initialise the Tk objects it needs, and traverses object references to get from the turtle to the Tk root window.
Both answers then set the Tk root window's transparency. Which method is more useful depends on your usecase.
Code (made to be as similar to cdlane's as possible):
from turtle import *
turtle = getturtle()
# This does not have to come before the drawing code --
# you can alter window transparency at any moment.
root = (turtle
._screen
.getcanvas()
.winfo_toplevel())
root.attributes('-alpha', 0.3)
turtle.width(10)
turtle.penup()
turtle.sety(-100)
turtle.pendown()
turtle.circle(100)
The title says it all. How to move the entire window to a place on the screen using tkinter. This should be moving the root frame.
Use the geometry method of the root (or any Toplevel) window. For example:
import tkinter as tk
root = tk.Tk()
root.geometry("+200+400") # places the window at 200,400 on the screen
use this:
from tkinter import Tk
main=Tk()
main.geometry('+100+200')
main.mainloop()
or do it with function :
def change_position(root_variable,x,y):
root_variable.geometry('+{}+{}'.format(x,y))
and use :change_position(main,500,400)
edit: added dot for format
I've been trying to create a basic gui using tkinter, I've done it before on a different computer but for some reason its invisible. Is there something wrong with my code or the computer (windows)?
import sys
from tkinter import *
mygui = Tk()
mygui.geometry('300x300+0+982')
mygui.title("my gui")
mygui.mainloop()
Is your screen height is bigger than 982 pixel?
Following line place the window at (0, 982) with width 300, height 300. If your screen height smaller than 982 pixel, you can't see it.
mygui.geometry('300x300+0+982')
Replace it with following:
mygui.geometry('300x300+0+0')
and you will see it.
pythonware.com/library/tkinter/introduction/…
documents a overrideredirect method
that will remove thetitlebar and
borders, if that is not enough you
must set the native window style, I'm
not sure if Tkinter gives you that
kind of low-level access, if not, try
the something like
twapi.magicsplat.com/ui.html#set_window_style
TCL extension
In an earlier post I got this as a reply on how to get a border in Tkinter similar to the one pictured below. I am not familiar with Tcl and it's extensions. So how would go about doing this? The end goal is basicaly to get the border below on a Tkinter window.
Edit :
I used the following on Windows 7 and it didn't seem to change the style. It's probably missing something. Any help would be appreciated, this could be really cool!
import string, win32ui, win32con
import Tkinter as tk
root = tk.Tk()
frame = win32ui.CreateWindowFromHandle(string.atoi(root.wm_frame(), 0))
frame.ModifyStyle(win32con.WS_CAPTION, 0, win32con.SWP_FRAMECHANGED)
root.mainloop()
You can do this using a combination of the Python win32 api packages and Tkinter. What you need to know is that a Tk window is the client section of a Win32 window. The window manager interactions are handled using a wrapper that is the parent of Tk window itself. If you have a Tkinter window 'w' then you can create a PyWin32 window for the frame or just manipulate it directly. You can get the frame hwnd using w.wm_frame() and parsing the hex string returned or by using GetParent on the winfo_id value from the Tk window (although wm_frame is likely to be more reliable).
import string, win32ui, win32con
from Tkinter import *
w = Tk()
frame = win32ui.CreateWindowFromHandle(string.atoi(w.wm_frame(), 0))
frame.ModifyStyle(win32con.WS_CAPTION, 0, win32con.SWP_FRAMECHANGED)
This removes the WS_CAPTION style and notifies the window that its frame is modified which forces a geometry recalculation so that the change propagates to the Tk child window.
EDIT ---
The following arranges to ensure we modify the window style after the window has been fully created and mapped to the display.
import string, win32ui, win32con
from Tkinter import *
def decaption(event):
w = event.widget
frame = win32ui.CreateWindowFromHandle(string.atoi(w.wm_frame(), 0))
frame.ModifyStyle(win32con.WS_CAPTION, 0, win32con.SWP_FRAMECHANGED)
w.bind("<Map>", None)
root = Tk()
root.bind("<Map>", decaption)
root.mainloop()
One solution is to draw your own border. Use overrideredirect to remove all decorations, grid/pack/place a canvas that fills the window, then draw or use bitmaps to get the visual effect you want. You'll have to add your own bindings for moving and resizing tne window, but that's not too difficult.