multiplying moving rectangles in python (tkinter) - python

I have two questions:
I want to make several rectangles, moving randomly. I am at a point where i
can do it with one rectangle but i don't get it how to multiply them.
I am a beginner so i have copied this example and modified it in my favor but i don't know exactly why i have to write everytime the "self" and the "init". It seems to be common to name those parameters in this manner.
I looked both questions up several times but didn't find a satisfying answer.
here the code:
from tkinter import *
from tkinter.ttk import *
from random import *
class simulation:
def __init__(self, anzahl, master = None):
self.master = master
self.canvas = Canvas(master, width= 2736, height= 1824)
self.rectangle = self.canvas.create_rectangle(500, 380, 515, 395, fill = "black")
self.canvas.pack()
self.movement()
def movement(self):
self.canvas.move(self.rectangle, randint(-10,10), randint(-10,10))
self.canvas.after(100, self.movement)
if __name__ == "__main__":
master = Tk()
master.title("Simulation")
simulation = simulation(master)
mainloop()

maybe this will help you, make an object for each player and the canvas packed ones in order not to hide other players ...
from tkinter import *
from random import *
class simulation:
def __init__(self, master , canvas , color):
self.master = master
self.canvas = canvas
self.rectangle = canvas.create_rectangle(500, 380, 515, 395, fill=color)
def movement(self):
canvas.move(self.rectangle, randint(-10,10), randint(-10,10))
self.canvas.after(100, self.movement)
if __name__ == "__main__":
master = Tk()
canvas = Canvas(master, width=2736, height=1824)
canvas.pack()
master.title("Simulation")
player1 = simulation(master, canvas,"red")
player2 = simulation(master,canvas, "black")
player1.movement()
player2.movement()
mainloop()

Related

Create an image in a canvas inside a class [duplicate]

This question already has answers here:
Why does Tkinter image not show up if created in a function?
(5 answers)
Closed 1 year ago.
I wanted to create a chess program using OOP. So I made a superclass Pieces, a subclass Bishop, and a UI class GameUI. I created a canvas in the class GameUI. I wanted, that when I instantiate an object bishop in the class GameUI, it shows an Image from a bishop, on the canvas.
The problem is, when I instantiate the Bishop, I don't see any image. So I tried to do the same with a text : instead of using the method create_image from the class Canvas, I used the method create_text, and it worked : I saw a text on the canvas. That means, the problem comes from the method create_image, and I don't understand it.
If I create an Image directly in the class GameUi, it works! but that's not what I want...
So I don't have any error message. I see the canvas (with a blue background), but no image on it.
Here's the code :
from tkinter import PhotoImage, Tk, Canvas
class Pieces:
def __init__(self, can, color, x_position, y_position):
self.color = color
self.x_position = x_position
self.y_position = y_position
class Bishop(Pieces):
def __init__(self, can, color, x_position, y_position):
super().__init__(can, color, x_position, y_position)
if color == "black":
icon_path = 'black_bishop.png'
elif color == "white":
icon_path = 'white_bishop.png'
icon = PhotoImage(file=icon_path) # doesn't see the image
can.create_image(x, y, image=icon)
class GameUI:
def __init__(self):
self.windows = Tk()
self.windows.title("My chess game")
self.windows.geometry("1080x720")
self.windows.minsize(300, 420)
self.can = Canvas(self.windows, width=1000, height=600, bg='skyblue')
icon = PhotoImage(file=icon_path) # here I create the image in this class, and
can.create_image(x, y, image=icon) # we can see it very well
self.bishop = Bishop(self.can, "black", 50, 50)
self.can.pack()
self.windows.mainloop()
app = GameUI()
To make your code work, I decided to sort of rewrite it based on this answer. It works now, but really the only thing that you needed to add was self.icon instead of icon. icon gets garbage collected since there is no further reference to it, while self.icon remains. Also, it's not entirely the same as yours was, so it probably needs a bit of rewriting too.
from tkinter import *
from random import randint
class Piece:
def __init__(self, canvas, x1, y1):
self.x1 = x1
self.y1 = y1
self.canvas = canvas
class Bishop(Piece):
def __init__(self, canvas, x1, y1, color):
super().__init__(canvas, x1, y1)
if color == "black":
icon_path = 'black_bishop.png'
elif color == "white":
icon_path = 'white_bishop.png'
self.icon = PhotoImage(file=icon_path)
self.ball = canvas.create_image(self.x1, self.y1, image=self.icon)
def move_piece(self):
deltax = randint(0,5)
deltay = randint(0,5)
self.canvas.move(self.ball, deltax, deltay)
self.canvas.after(50, self.move_piece)
class GameUI:
def __init__(self):
# initialize root Window and canvas
root = Tk()
root.title("Chess")
root.resizable(False,False)
canvas = Canvas(root, width = 300, height = 300)
canvas.pack()
# create two ball objects and animate them
bishop1 = Bishop(canvas, 10, 10, 'white')
bishop2 = Bishop(canvas, 60, 60, 'black')
bishop1.move_piece()
bishop2.move_piece()
root.mainloop()
app = GameUI()

Having difficulties placing a frame within a frame with Tkinter

I am having issues trying to place a frame within a frame using classes/objects with python/tkinter. My goal is to simply place a frame in the north west corner of the outer frame instead of the whole window itself. I think I'm incorrectly referencing the outer frame in the inner frame class but I'm not 100% sure. I am fairly new to OOP and tkinter so forgive my ignorance and I appreciate the help.
Sample code:
from tkinter import *
class window():
def __init__(self, master):
self.master = master.minsize(500, 500)
master.maxsize(500,500)
self.outer_frame = Frame(master, width = 250, height = 250, bg = "red").place(anchor = CENTER, relx = 0.5, rely = 0.5)
def create_inner_window(self):
self.inner_frame = inner_frame(self.outer_frame)
class inner_frame():
def __init__(self, outer_frame):
self.inner_frame = Frame(master = outer_frame, width = 125, height = 125, bg = "blue").place(anchor = NW)
root = Tk()
my_window = window(root)
my_window.create_inner_window()
root.mainloop()
What I'm trying to accomplish:
What I get instead:

Does pyautogui.keydown() not register as an event in python's bind() function?

I am making a game in which you dodge falling object by using the tkinter library. In my code, I am trying to make an object fall by binding a canvas.move() function with pressing the down arrow key, then using pyautogui to hold down the key. Here is my code:
from tkinter import *
from random import randint
import pyautogui
class Window(Frame):
def __init__(self, master=None):
Frame.__init__(self, master)
self.master = master
self.initWindow()
def initWindow(self):
self.master.title('Dodger')
self.pack(fill=BOTH, expand=1)
self.master.geometry('600x800')
self.master.config(bg='black')
menu = Menu(self.master)
self.master.config(menu=menu)
def clientExit():
exit()
file = Menu(menu)
file.add_command(label='Exit', command=clientExit)
file.add_command(label='Start', command=self.game)
menu.add_cascade(label='File', menu=file)
def game(self):
canvas = Canvas(self.master, width='600', height='800', borderwidth='0', highlightthickness='0')
canvas.pack()
canvas.create_rectangle(0, 0, 600, 800, fill='black', outline='black')
character = canvas.create_rectangle(270, 730, 330, 760, fill='magenta', outline='cyan', width='2')
def left(event):
cord = canvas.coords(character)
if not cord[0] <= 5:
canvas.move(character, -10, 0)
def right(event):
cord = canvas.coords(character)
if not cord[2] >= 595:
canvas.move(character, 10, 0)
self.master.bind('<Left>', left)
self.master.bind('<Right>', right)
class variables:
sizeMin = 10
sizeMax = 80
y = 10
minX = 5
maxX = 545
def createShape():
size = randint(variables.sizeMin, variables.sizeMax)
x = randint(variables.minX, variables.maxX)
topLeft = [x, variables.y]
bottomRight = [x + size, variables.y + size]
shape = canvas.create_rectangle(topLeft[0], topLeft[1], bottomRight[0], bottomRight[1],
fill='red', outline='red')
return shape
def moveShape(event):
cord = canvas.coords(x)
if cord[1] != 800:
canvas.move(x, 0, 10)
x = createShape()
self.master.bind('<Down>', moveShape)
pyautogui.keyDown('down')
root = Tk()
app = Window(root)
app.mainloop()
As you can see, at the bottom of the class, I binded the down arrow key and moving a shape down. However, the pyautogui does not work; the object does not move down unless I manually press the down arrow key. Am I forgetting something or is pyautogui not compatible with bind()? I know there are more efficient ways to move the object down, however with all the methods I have tried, none show the actual movement of the object heading down the screen; they just show the object being re-created in another position. Please let me know how I can fix this.
I wouldn't bother with it. Maybe it misses the _all argument. Try to simply bind canvas.move() function to a canvas.bind_all()

'RawTurtle' object has no attribute 'Turtle'

I saw the sorting example in turtle demo which python includes and I would like to add similar animations in my program. My program is based in tkinter and I would like to insert turtle animations in a tkinter canvas (with RawTurtle) so first I tried to create a black box in the canvas and I get the following error message:
AttributeError: 'RawTurtle' object has no attribute 'Turtle'
Here's my code:
import tkinter
from turtle import *
class MyApp():
def __init__(self, parent):
self.p = parent
self.f = tkinter.Frame(self.p).pack()
self.c = tkinter.Canvas(self.f, height = '640', width = '1000')
self.c.pack()
self.t = RawTurtle(self.c)
self.main(5)
def main(self, size):
self.t.size = size
self.t.Turtle.__init__(self, shape="square", visible=False)
self.t.pu()
self.t.shapesize(5, 1.5, 2)
self.t.fillcolor('black')
self.t.st()
if __name__ == '__main__':
root= tkinter.Tk()
frame = MyApp(root)
root.mainloop()
You pretty much have it -- those two settings you were trying to change via the non-existent Turtle() instance method can be handled when creating the RawTurtle:
import tkinter
from turtle import RawTurtle
class MyApp():
def __init__(self, parent):
self.p = parent
self.f = tkinter.Frame(self.p).pack()
self.c = tkinter.Canvas(self.f, height=640, width=1000)
self.c.pack()
self.t = RawTurtle(self.c, shape='square', visible=False)
self.main(5)
def main(self, size):
self.t.size = size # does nothing if stamping with pen up
self.t.penup()
self.t.shapesize(5, 1.5, 2)
self.t.fillcolor('black') # the default
self.t.stamp()
if __name__ == '__main__':
root = tkinter.Tk()
frame = MyApp(root)
root.mainloop()

tkinter canvas: text object variable font size?

I'm making some pretty pictures using a tkinter canvas and overlaying text on top of circles like in the following picture:
http://static.guim.co.uk/sys-images/Guardian/Pix/pictures/2012/11/6/1352220546059/Causes-of-deaths-graphic-008.jpg
I want the font size to be dependent on the same number that the circle size is dependent on.
tempfont = tkFont.Font(family='Helvetica',size=int(round(ms*topnode[1])))
self.display.create_text(center[0],center[1],fill = "#FFFFFF",text = int(round(ms*topnode[1])),font = tempfont)
My problem is that when I use the above code, the overlayed text is a constant size for every text object. The text itself is right, as in it displays the number that I want the font size to be, just not in the correct font size. I've experimented with putting in constant integers in the size definition (works as it's supposed to), and adding a del(tempfont) immediately after the above 2 lines of code, but I haven't found what fixes this problem yet.
What am I doing wrong?
Here's a self-contained little program that reproduces the problem:
from Tkinter import *
import tkFont
class TestApp(Frame):
def __init__(self, master=None, height = 160, width = 400):
Frame.__init__(self, master)
self.grid()
self.createWidgets()
def createWidgets(self):
self.display = Canvas(self, width = 800, height = 320, bg = "#FFFFFF")
self.display.grid(row=0,column=0)
def recurtext(tsize):
if tsize > 20:
recurtext(tsize-10)
tempfont = tkFont.Font(family='Helvetica',size=tsize)
self.display.create_text(800 - (tsize*12),160, text = str(tsize), font = tempfont)
recurtext(60)
app = TestApp()
app.master.title("Test")
app.mainloop()
The gist is that recurtext resizes the font recursively, and shows writes out the font size in that size... or I think it should. Maybe this is a bug with tkinter, but I'm still holding on to some hope that I'm the one who made a mistake in the logic here.
I've never run across this behavior before; it looks like a Tkinter bug. The good news is, there appears to be a workaround. If you give each font a unique name the problem seems to vanish.
The following example shows multiple lines, each with a different font size:
import Tkinter as tk
import tkFont
class Example(tk.Frame):
def __init__(self, parent):
tk.Frame.__init__(self, parent)
self.display = tk.Canvas(self, width=400, height=600, background="black")
self.display.pack(side="top", fill="both", expand=True)
y = 10
for size in range (2, 38, 2):
tempfont = tkFont.Font(family='Helvetica',size=size,
name="font%s" % size)
self.display.create_text(10, y, fill = "#FFFFFF",text = size,
font = tempfont, anchor="nw")
y = y + tempfont.metrics()["linespace"]
if __name__ == "__main__":
root = tk.Tk()
frame = Example(parent=root)
frame.pack(side="top", fill="both", expand=True)
root.mainloop()

Categories

Resources