Someone has written a really simple "paint" program in Python and I want to modify it a little bit. Some my question is. How can implement a rotate function to this program? I want to be able to rotate the window that the program is represented in 90 degrees to the right. Is this possible? Would it also be possible to implement a function to remove the border from the window. (It's the gui window I'm talking about).
from Tkinter import *
"""paint.py: not exactly a paint program.. just a smooth line drawing demo."""
b1 = "up"
xold, yold = None, None
def main():
root = Tk()
drawing_area = Canvas(root)
drawing_area.pack()
drawing_area.bind("<Motion>", motion)
drawing_area.bind("<ButtonPress-1>", b1down)
drawing_area.bind("<ButtonRelease-1>", b1up)
root.mainloop()
def b1down(event):
global b1
b1 = "down" # you only want to draw when the button is down
# because "Motion" events happen -all the time-
def b1up(event):
global b1, xold, yold
b1 = "up"
xold = None # reset the line when you let go of the button
yold = None
def motion(event):
if b1 == "down":
global xold, yold
if xold is not None and yold is not None:
event.widget.create_line(xold,yold,event.x,event.y,smooth=TRUE)
# here's where you draw it. smooth. neat.
xold = event.x
yold = event.y
if __name__ == "__main__":
main()
The solution was to just rotate the screen in Linux. I managed to do this with the command:
xrandr --output HDMI1 --rotate right
'Rotating' your screen, so that a different edge of the screen is treated as the top edge, is something that the operating system has to do. For instance, on Win 7, right click on screen, select Resolution, then select Orientation. The choices are Landscape (normal), Portrait (left edge, starting from Landscape) is top), Inverted Landscape, and Inverted Portrait. This works even if screen is not physically rotated -- but is a bit weird since cursor movement assumes that the screen is rotated.
Why exactly do you want to rotate the window? is it just because of the geometry? if so you could just resize the canvas to give the appearance of having been rotated using width and height arguments when it is created (or using configure on it after its been created)
and to remove the border (and the titlebar with it) you can use:
root.overrideredirect(True)
in your main()
Related
There are tons of results on centering a top level window comparative to your screen size. However, to my knowledge, there is no information out there on how to center the top level window in the center of the main window. I could hard code it but it's ugly to do and would of course not work anymore once im moving the main window to a seperate screen. So my question is, how do i center a top level window in the main window regardless of the position of the main window?
from tkinter import Toplevel, Button, Tk
root = Tk()
width = 960
height = 540
root.geometry("%dx%d" % (width, height))
def new_window() :
win = Toplevel()
win.geometry("%dx%d+%d+%d" % (480, 270, root.winfo_x() + width/4, root.winfo_y() + height/4))
#the geometry method can take four numbers as a string argument
#first two numbers for dimensions
#second two numbers for the position of the opened window
#the position is always the top left of your window
#winfo_x and winfo_y are two methods
#they determine the current position of your window
Button(root, text = "open", command = new_window).pack()
root.mainloop()
You can test the code and make it satisfy your needs.
I hope that helps !
Get location of main window along with dimensions
x, y, w_main, h_main
get dimensions of your top level window
w_top, h_top
location of toplevel is in center of main window, so its relative position is:
x_rel = round((w_main - w_top)/2)
y_rel = round((h_main - h_top)/2)
now add these relative coordinated to absolute location of main window:
x += x_rel
y += y_rel
Place the toplevel at these coordinates
x, y
repeat the whole process every time the location of main window changes, except if dimensions are fixed they can be separated out (no need to check them repeatedly)
Is it possible to change the position of the turtle console on screen?
My main objective is to write code that can move the window, that's all.
I'm using Python 3.4.0 under Windows 10.
If any extra information is needed please ask.
Why do folks always jump into tkinter before reading the turtle documentation?
Yes, you can set the screen position of the turtle graphics window using the same setup() method you use to size it:
from turtle import Turtle, Screen
def animate():
global offset
screen.setup(width=0.333, height=0.333, startx=offset, starty=offset)
turtle.dot(offset)
offset += 10
if offset < 300:
screen.ontimer(animate, 100)
screen = Screen()
turtle = Turtle()
offset = 30
animate()
screen.exitonclick()
startx, if positive, is the starting position in pixels from the left edge of the screen, or from the right edge if negative. Similarly, starty, if positive, is the starting position from the top edge of the screen, or from the bottom edge if negative. By default, the window is centered on the screen.
Your title asks about the position of the Turtle Graphics window on the screen but the body of your question asks about the Turtle Console. These might be considered two different windows.
My main objective is to write code that can move the window
I can't tell if you just want to set the initial position of the window or actually move the window around the screen so I rewrote my example to demonstrate the later.
Yes. You need to get the root window that contains the Tkinter Canvas that the turtle is using as its TurtleScreen. Once you have that window you can change its geometry.
Here's a simple demo.
import turtle
turtle.setup(width=0.5, height=0.5)
screen = turtle.Screen()
width, height = screen.window_width(), screen.window_height()
canvas = screen.getcanvas()
left, top = 30, 100
geom = '{}x{}+{}+{}'.format(width, height, left, top)
canvas.master.geometry(geom)
t = turtle.Turtle()
turtle.exitonclick()
I'm using Pyglet and I have a little that includes an object moving over a background. Both of them are represented by images (png and jpg).
I've created a non-fullscreen window with size 800x600 and it works fine, but when I toggle to fullscreen... background and object have the same size as before and the rest of the screen is filled with black (empty color).
What I want to do is to "scale" the images or change the resolution when I toggle fullscreen mode.
I've read the documentation, but I can't find the answer to this.
I know that with Pygame, this problem solves itself automatically (if you change the window size, everything rescales automatically)... but how do you do this with pyglet?
This is my relevant code:
import pyglet
WIDTH = 800
HEIGHT = 600
working_dir = '/where/i/have/my/images/'
window = pyglet.window.Window(WIDTH, HEIGHT)
background = pyglet.image.load(working_dir + 'background.jpg')
flying_thing = pyglet.image.load(working_dir + 'flying_thing.png')
#window.event
def on_draw():
window.clear()
background.blit(0, 0)
flying_thing.blit(WIDTH // 2, HEIGHT // 2)
#window.event
def on_key_press(symbol, modifiers):
if symbol == pyglet.window.key.SPACE:
window.set_fullscreen(not window.fullscreen)
pyglet.app.run()
You can try this code changing working_dir, background.jpg and flying_thing.png to a working directory of yours and two images in it.
I didn't tried, but from pyglet docs, blit supports width and height. Its signature is
blit(self, x, y, z=0, width=None, height=None)
Have you tried using
background.blit(width=window.width, height=windows.height)
instead? (I'm not sure the window.width changes on full_screen, let's see...).
This answer can also be relevant to your question: https://stackoverflow.com/a/11183462/931303.
I've been trying to create a graph using a create_line and a list of (x,y) points.
import Tkinter
Screen = [a list of screen coordinates]
World = []
for x,y in screen:
World.append(somefunctiontochange(x,y))
if len(World) >= 2:
Canvas.create_line(World)
The line doesn't show in my canvas though, and no error was given. Any help?
Took me a while but this is how you draw to a canvas in the way you want:
import Tkinter as tk
root = tk.Tk()
root.geometry("500x500")
root.title("Drawing lines to a canvas")
cv = tk.Canvas(root,height="500",width="500",bg="white")
cv.pack()
def linemaker(screen_points):
""" Function to take list of points and make them into lines
"""
is_first = True
# Set up some variables to hold x,y coods
x0 = y0 = 0
# Grab each pair of points from the input list
for (x,y) in screen_points:
# If its the first point in a set, set x0,y0 to the values
if is_first:
x0 = x
y0 = y
is_first = False
else:
# If its not the fist point yeild previous pair and current pair
yield x0,y0,x,y
# Set current x,y to start coords of next line
x0,y0 = x,y
list_of_screen_coods = [(50,250),(150,100),(250,250),(350,100)]
for (x0,y0,x1,y1) in linemaker(list_of_screen_coods):
cv.create_line(x0,y0,x1,y1, width=1,fill="red")
root.mainloop()
You need to supply create_line with the x,y positions at the start and end point of the line, in the example code above (works) I'm drawing four lines connecting points (50,250),(150,100),(250,250),(350,100) in a zigzag line
Its worth pointing out also that the x,y coords on a canvas start at the top left rather than the bottom left, think of it less like a graph with the x,y = 0,0 in the bottom left of the canvas and more how you would print to a page starting in top left corner moving to the right in the x and with the y incrementing as you move down the page.
I used:
http://www.tutorialspoint.com/python/tk_canvas.htm as reference.
If you aren't getting errors and you're certain your function is being called, you probably have one of three problems:
Is your canvas visible? A common mistake for beginners is to either forget to pack/grid/place the canvas, or to neglect to do that for all of its containers. An easy way to verify is to temporarily give your canvas a really bright background so that it stands out from the rest of the GUI.
Have you set the scroll region? The other explanation is that the drawing is happening, but it's happening in an area that is outside the viewable portion of the canvas. You should be setting the scrollregion attribute of the canvas after creating your drawings, to make sure everything you're drawing can be made visible.
Does your canvas and canvas objects have an appropriate color? It's possible that you've changed the background of the canvas to black (since you don't show that code in your question), and you're using a default color of black when creating your lines.
Is there any way to make the cursor (the turtle) go to the right , left, down and upper edge of the canvas?
Someone suggested to use turtle.setx() with argument as 0 to move to leftmost position. But when I do so, the turtle is moved to the default position (to the center).
Use the window_width() and window_height() functions to determine the size of the window:
This moves the turtle to the right-most edge:
import turtle as tt
def main():
tt.reset()
print(tt.window_width(), tt.window_height())
tt.setx(tt.window_width()//2)
if __name__ == '__main__':
main()
tt.mainloop()