I'm trying to move a frame on my GUI, but I am unable to do so because of my classes. How can I move the frame2 with a
Slider1.SetVal(x=20, y=30)
while keeping my classes.
My Code
class Slider:
def __init__ (self,Name, x, y, Height, Width, KnobHeight, KnobWidth, LowVal, HighVal, BgColor, FgColor):
self.Name = Name
self.x = x
self.y = y
self.Height = Height
self.Width = Width
self.KnobHeight = KnobHeight
self.KnobWidth = KnobWidth
self.LowVal = LowVal
self.HighVal = HighVal
self.BgColor = BgColor
self.FgColor = FgColor
def Display(self):
frame = Frame(self.Name, width=self.Width, height=self.Height, colormap="new", bg=self.BgColor)
frame.place(x=self.x, y=self.y)
frame2 = Frame(self.Name, width=self.KnobWidth, height=self.KnobHeight, colormap="new", bg=self.FgColor)
frame2.place(x=(self.x + self.Width/2 - self.KnobWidth/2), y=(self.Height - self.KnobHeight/2 + self.y))
def SetVal(self):
Main.update()
frame2.place(x=33, y=5)
window1 = Window(Main)
Slider1 = Slider(Main,100,60,200,15,15,35,0,80,"White","Green")
Slider1.Display()
Slider1.SetVal(3,30)
Main.mainloop()
Related
I am trying to have the rectangle I create spawn at the bottom of my tkinter canvas instead of spawning at the top without affecting its size. I have only found this post and this doesn't help me as it does not describe how to move the shape to the bottom of the canvas
Here's my code:
from tkinter import *
from tkinter.ttk import *
#Temporary
width = 1024
height=768
class GFG:
def __init__(self, master = None):
self.master = master
self.master.configure(bg="black") #Apart from the canvas colour
self.x = 0
self.y = 0
self.canvas = Canvas(master, background="black", highlightthickness=0, width=width, height=height) #Canvas colour
self.rectangle = self.canvas.create_rectangle(5, 5, 25, 25, fill='white')
self.canvas.pack()
self.movement()
def movement(self):
self.canvas.move(self.rectangle, self.x, self.y)
self.canvas.after(100, self.movement)
def left(self, event):
print(event.keysym)
self.x = -5
self.y = 0
def right(self, event):
print(event.keysym)
self.x = 5
self.y = 0
if __name__ == "__main__":
master = Tk()
master.title("Black Hole Settlers")
gfg = GFG(master)
master.bind("<KeyPress-Left>", lambda e: gfg.left(e))
master.bind("<KeyPress-Right>", lambda e: gfg.right(e))
mainloop()
Pastebin
Thanks,
swanserquack
So I was trying to figure out who my coordinates were messed up when adding text to an image using canvas coordinates , until I noticed that (0, 0) is off:
How would I put the origin of the canvas in the same place as Pillow does(or at least account for this when adding the text)?
class PictureWindow(tk.Toplevel):
def __init__(self, parent):
tk.Toplevel.__init__(self, parent)
self.parent = parent
self.x = self.y = 0
self.rect = None
self.tex = None
self.start_x = None
self.start_y = None
image = Image.open(file)
smaller_image = image.resize((round(image.size[0]/2), round(image.size[1]/2)))
img = ImageTk.PhotoImage(smaller_image)
self.canvas = tk.Canvas(self, width=img.width(), height=img.height())
self.canvas.img = img
self.canvas.create_image(0, 0, image=img, anchor=tk.NW)
self.canvas.pack(expand=True)
self.canvas.create_text((0, 0), text="(0, 0)", fill=self._from_rgb((225, 225, 225)))
self.canvas.bind("<ButtonPress-1>", self.on_button_press)
self.canvas.bind("<B1-Motion>", self.on_move_press)
self.canvas.bind("<ButtonRelease-1>", self.on_button_release)
self.finished = tk.Button(self, text="Add Watermark", command=self.Watermark)
self.finished.pack()
self.go_back = tk.Button(self, text="Go back to change settings", command=self.go_away)
self.go_back.pack()
def _from_rgb(self, rgb):
r, g, b = rgb
return f'#{r:02x}{g:02x}{b:02x}'
def go_away(self):
self.withdraw()
def on_button_press(self, event):
self.start_x = event.x
self.start_y = event.y
self.canvas.delete(self.tex)
self.tex = None
if not self.rect:
self.rect = self.canvas.create_rectangle(self.x, self.y, 1, 1, fill=self._from_rgb((249, 0, 0)), stipple='gray12')
def on_move_press(self, event):
self.curX, self.curY = (event.x, event.y)
self.text_x = ((self.start_x + self.curX) / 2)
self.text_y = ((self.start_y + self.curY) / 2)
self.canvas.coords(self.rect, self.start_x, self.start_y, self.curX, self.curY)
def on_button_release(self, event):
font_preview_size = int(font_size)
font_preview_almost = (font_preview_size)
font_preview = int(font_preview_almost)
if not self.tex:
self.tex = self.canvas.create_text((self.text_x, self.text_y), text=watermark_text, font=('Gotham Medium', font_preview), fill=self._from_rgb((color)))
def Watermark(self):
self.font_size_var = font_size
Font_Size = int(self.font_size_var)
A = alpha
R = int(color[0])
B = int(color[1])
G = int(color[2])
img = Image.open(file).convert("RGBA")
img_down = img.resize((int(img.width/2), int(img.height/2)), resample=Image.NEAREST)
img_down.x, img_down.y = img_down.size
txt = Image.new('RGBA', img_down.size, (225,225,225,0))
if custom_font == "yes":
font = ImageFont.truetype(font_file, Font_Size)
draw = ImageDraw.Draw(txt)
text = watermark_text
if custom_font == "no":
font = ImageFont.truetype('arial.ttf', Font_Size)
draw = ImageDraw.Draw(txt)
text = watermark_text
self.final_x = self.canvas.canvasx(self.text_x)
self.final_y = self.canvas.canvasx(self.text_x)
draw.text((self.final_x, self.final_y), text, font=font, fill=(R, G, B, A))
comp = Image.alpha_composite(img_down, txt)
img_up = comp.resize((int(comp.width*2), int(comp.height*2)), resample=Image.NEAREST)
img_up.save(save_file)
img_up.show()
p.s. if anyone knows why when the pillow text alpha is set to 0, the text is never added, please let me know!
I'll just post Bryan Oakley's comment as an answer. Pillow has an anchor option and all I needed to do was set it to mm
I used the code below which I found on internet to implement a mouse hovering action in python:
from tkinter import *
import numpy as np
class rect:
def __init__(self, root):
self.root = root
self.size = IntVar()
self.canvas = Canvas(self.root, width=800, height=300)
self.scale = Scale(self.root, orient=HORIZONTAL, from_=3, to=20, tickinterval=1, variable=self.size)
self.scale.bind('<ButtonRelease>', self.show)
self.canvas.bind('<Motion>', self.motion)
self.board = []
self.array = np.zeros((self.scale.get(),self.scale.get())).tolist()
self.canvas.pack()
self.scale.pack()
def motion(self,event):
if self.canvas.find_withtag(CURRENT):
current_color = self.canvas.itemcget(CURRENT, 'fill')
self.canvas.itemconfig(CURRENT, fill="cyan")
self.canvas.update_idletasks()
self.canvas.after(150)
self.canvas.itemconfig(CURRENT, fill=current_color)
def show(self,event):
self.canvas.delete('all')
x = 50
y = 50
row = []
self.board.clear()
for i in range(self.scale.get()):
row = []
for j in range(self.scale.get()):
rectangle = self.canvas.create_rectangle(x, y, x + 50, y + 50, fill='red')
x += 50
row.append(rectangle)
x -= j*50
y +=50
self.board.append(row)
print(self.board)
root = Tk()
a = rect(root)
root.mainloop()
The problem with the execution is that the color of the item changes to blue only for a limited time.
I need the color of each item in the canvas to be changed whenever I enter its zone and remain blue until the mouse is leaving the item.
You can pass the argument activefill when creating your rectangle.
From effboot.org:
Fill color to use when the mouse pointer is moved over the item, if
different from fill.
To do so, replace:
rectangle = self.canvas.create_rectangle(x, y, x + 50, y + 50, fill='red')
By:
rectangle = self.canvas.create_rectangle(x, y, x + 50, y + 50, fill='red', activefill='cyan')
This removes the need to bind Motion to your canvas, and also makes the code noticebly shorter:
from tkinter import *
import numpy as np
class rect:
def __init__(self, root):
self.root = root
self.size = IntVar()
self.canvas = Canvas(self.root, width=800, height=300)
self.scale = Scale(self.root, orient=HORIZONTAL, from_=3, to=20, tickinterval=1, variable=self.size)
self.scale.bind('<ButtonRelease>', self.show)
self.board = []
self.array = np.zeros((self.scale.get(),self.scale.get())).tolist()
self.canvas.pack()
self.scale.pack()
def show(self,event):
self.canvas.delete('all')
x = 50
y = 50
row = []
self.board.clear()
for i in range(self.scale.get()):
row = []
for j in range(self.scale.get()):
rectangle = self.canvas.create_rectangle(x, y, x + 50, y + 50, fill='red', activefill='cyan')
x += 50
row.append(rectangle)
x -= j*50
y +=50
self.board.append(row)
print(self.board)
root = Tk()
a = rect(root)
root.mainloop()
I changed motion method and added self.last = None to __init__ method:
from tkinter import *
import numpy as np
class rect:
def __init__(self, root):
self.root = root
self.size = IntVar()
self.canvas = Canvas(self.root, width=800, height=300)
self.scale = Scale(self.root, orient=HORIZONTAL, from_=3, to=20, tickinterval=1, variable=self.size)
self.scale.bind('<ButtonRelease>', self.show)
self.canvas.bind('<Motion>', self.motion)
self.board = []
self.array = np.zeros((self.scale.get(),self.scale.get())).tolist()
self.canvas.pack()
self.scale.pack()
self.last = None
def motion(self, event):
temp = self.canvas.find_withtag(CURRENT)
if temp == self.last:
self.canvas.itemconfig(CURRENT, fill="cyan")
self.canvas.update_idletasks()
else:
self.canvas.itemconfig(self.last, fill="red")
self.last = temp
def show(self,event):
self.canvas.delete('all')
x = 50
y = 50
row = []
self.board.clear()
for i in range(self.scale.get()):
row = []
for j in range(self.scale.get()):
rectangle = self.canvas.create_rectangle(x, y, x + 50, y + 50, fill='red')
x += 50
row.append(rectangle)
x -= j*50
y +=50
self.board.append(row)
print(self.board)
root = Tk()
a = rect(root)
root.mainloop()
I have this code which lets the user draw on the canvas and save it as a jpeg file.
As mentioned in this post, I tried to draw in parallel on the canvas and in memory using the PIL so that I can save it as a jpeg instead of postscript. It seemed to be working until I found out that some of the images I saved with PIL are not the same as what was drawn on the canvas.
I assume canvas.create_line and draw.line from the PIL image draw module function similarly and should give similar output.
Below is what went wrong:
For example, when I draw a "T" it seems alright (Left is my drawing, right is the saved image).
But when I draw an "A" , the output image seems a bit weird.
This is my current code:
import Tkinter as tk
import Image,ImageDraw
class ImageGenerator:
def __init__(self,parent,posx,posy,*kwargs):
self.parent = parent
self.posx = posx
self.posy = posy
self.sizex = 200
self.sizey = 200
self.b1 = "up"
self.xold = None
self.yold = None
self.coords= []
self.drawing_area=tk.Canvas(self.parent,width=self.sizex,height=self.sizey)
self.drawing_area.place(x=self.posx,y=self.posy)
self.drawing_area.bind("<Motion>", self.motion)
self.drawing_area.bind("<ButtonPress-1>", self.b1down)
self.drawing_area.bind("<ButtonRelease-1>", self.b1up)
self.button=tk.Button(self.parent,text="Done!",width=10,bg='white',command=self.save)
self.button.place(x=self.sizex/7,y=self.sizey+20)
self.button1=tk.Button(self.parent,text="Clear!",width=10,bg='white',command=self.clear)
self.button1.place(x=(self.sizex/7)+80,y=self.sizey+20)
self.image=Image.new("RGB",(200,200),(255,255,255))
self.draw=ImageDraw.Draw(self.image)
def save(self):
print self.coords
self.draw.line(self.coords,(0,128,0),width=3)
filename = "temp.jpg"
self.image.save(filename)
def clear(self):
self.drawing_area.delete("all")
self.coords=[]
def b1down(self,event):
self.b1 = "down"
def b1up(self,event):
self.b1 = "up"
self.xold = None
self.yold = None
def motion(self,event):
if self.b1 == "down":
if self.xold is not None and self.yold is not None:
event.widget.create_line(self.xold,self.yold,event.x,event.y,smooth='true',width=3,fill='blue')
self.coords.append((self.xold,self.yold))
self.xold = event.x
self.yold = event.y
if __name__ == "__main__":
root=tk.Tk()
root.wm_geometry("%dx%d+%d+%d" % (400, 400, 10, 10))
root.config(bg='white')
ImageGenerator(root,10,10)
root.mainloop()
Where did I go wrong and what should I do to save the exact same picture that is drawn on the canvas as a jpeg image?
I managed to solved my problem.In fact i was just being dumb. Even though canvas.create_line and draw.line have the similar function, i didn't use the same exact data to draw out both images. after making changes, this is my working code.
import Tkinter as tk
import Image,ImageDraw
class ImageGenerator:
def __init__(self,parent,posx,posy,*kwargs):
self.parent = parent
self.posx = posx
self.posy = posy
self.sizex = 200
self.sizey = 200
self.b1 = "up"
self.xold = None
self.yold = None
self.drawing_area=tk.Canvas(self.parent,width=self.sizex,height=self.sizey)
self.drawing_area.place(x=self.posx,y=self.posy)
self.drawing_area.bind("<Motion>", self.motion)
self.drawing_area.bind("<ButtonPress-1>", self.b1down)
self.drawing_area.bind("<ButtonRelease-1>", self.b1up)
self.button=tk.Button(self.parent,text="Done!",width=10,bg='white',command=self.save)
self.button.place(x=self.sizex/7,y=self.sizey+20)
self.button1=tk.Button(self.parent,text="Clear!",width=10,bg='white',command=self.clear)
self.button1.place(x=(self.sizex/7)+80,y=self.sizey+20)
self.image=Image.new("RGB",(200,200),(255,255,255))
self.draw=ImageDraw.Draw(self.image)
def save(self):
filename = "temp.jpg"
self.image.save(filename)
def clear(self):
self.drawing_area.delete("all")
self.image=Image.new("RGB",(200,200),(255,255,255))
self.draw=ImageDraw.Draw(self.image)
def b1down(self,event):
self.b1 = "down"
def b1up(self,event):
self.b1 = "up"
self.xold = None
self.yold = None
def motion(self,event):
if self.b1 == "down":
if self.xold is not None and self.yold is not None:
event.widget.create_line(self.xold,self.yold,event.x,event.y,smooth='true',width=3,fill='blue')
self.draw.line(((self.xold,self.yold),(event.x,event.y)),(0,128,0),width=3)
self.xold = event.x
self.yold = event.y
if __name__ == "__main__":
root=tk.Tk()
root.wm_geometry("%dx%d+%d+%d" % (400, 400, 10, 10))
root.config(bg='white')
ImageGenerator(root,10,10)
root.mainloop()
make a litte change about mouse event
"""
画板,鼠标移动速度太快可以造成不连续的点
tkinter 8.6.11
https://stackoverflow.com/questions/17915440/python-tkinter-save-canvas-as-image-using-pil
"""
import tkinter as tk
from PIL import Image, ImageDraw
class ImageGenerator:
def __init__(self, parent, posx, posy, *kwargs):
self.parent = parent
self.posx = posx
self.posy = posy
self.sizex = 280
self.sizey = 280
self.penColor = "white" # 画笔的颜色 (255, 255, 255)
self.backColor = "black" # 画布背景色 (0, 0, 0)
self.penWidth = 10 # 笔刷的宽度
self.drawing_area = tk.Canvas(
self.parent, width=self.sizex, height=self.sizey, bg=self.backColor
)
self.drawing_area.place(x=self.posx, y=self.posy)
self.drawing_area.bind("<B1-Motion>", self.motion)
self.button = tk.Button(
self.parent, text="Done", width=10, bg="white", command=self.save
)
self.button.place(x=self.sizex / 7, y=self.sizey + 20)
self.button1 = tk.Button(
self.parent, text="Clear", width=10, bg="white", command=self.clear
)
self.button1.place(x=(self.sizex / 7) + 80, y=self.sizey + 20)
self.image = Image.new("RGB", (self.sizex, self.sizey), (0, 0, 0))
self.draw = ImageDraw.Draw(self.image)
def save(self):
filename = "temp.jpg"
self.image.save(filename)
def clear(self):
"""将画板和image清空"""
self.drawing_area.delete("all")
self.image = Image.new("RGB", (self.sizex, self.sizey), (0, 0, 0))
self.draw = ImageDraw.Draw(self.image)
def motion(self, event):
"""在画板和image上同时绘制"""
self.drawing_area.create_oval(
event.x,
event.y,
event.x + self.penWidth,
event.y + self.penWidth,
fill=self.penColor,
outline=self.penColor,
) # 在画布上画
self.draw.ellipse(
(
(event.x, event.y),
(event.x + self.penWidth, event.y + self.penWidth),
),
fill=self.penColor,
outline=self.penColor,
width=self.penWidth,
) # 在生成的图上画,point、line都太细
if __name__ == "__main__":
root = tk.Tk()
root.wm_geometry("%dx%d+%d+%d" % (300, 350, 10, 10))
root.config(bg="white")
ImageGenerator(root, 10, 10)
root.mainloop()
I can track where the user clicks and where they release but I want to track distance traveled.
from Tkinter import *
root = Tk()
class DragCursor():
def __init__(self, location):
self.label = location
location.bind('<ButtonPress-1>', self.StartMove)
location.bind('<ButtonRelease-1>', self.StopMove)
def StartMove(self, event):
startx = event.x
starty = event.y
print [startx, starty]
def StopMove(self, event):
self.StartMove
stopx = event.x
stopy = event.y
print [stopx, stopy]
location = Canvas(root, width = 300, height = 300)
DragCursor(location)
location.pack()
root.mainloop()
You just need to use the distance formula for determining the distance between two points in an xy-plane,
Also, you need to include some kind of instance variable that will save the coordinates for the start and end points so that you can compute it after the mouse release.
This is pretty much your code just with a new distancetraveled function that is printed at the end of StopMove using self.positions.
from Tkinter import *
root = Tk()
class DragCursor():
def __init__(self, location):
self.label = location
location.bind('<ButtonPress-1>', self.StartMove)
location.bind('<ButtonRelease-1>', self.StopMove)
self.positions = {}
def StartMove(self, event):
startx = event.x
starty = event.y
self.positions['start'] = (startx, starty)
def StopMove(self, event):
stopx = event.x
stopy = event.y
self.positions['stop'] = (stopx, stopy)
print self.distancetraveled()
def distancetraveled(self):
x1 = self.positions['start'][0]
x2 = self.positions['stop'][0]
y1 = self.positions['start'][1]
y2 = self.positions['stop'][1]
return ((x2-x1)**2 + (y2-y1)**2)**0.5
location = Canvas(root, width = 300, height = 300)
DragCursor(location)
location.pack()
root.mainloop()