245 lines
8.1 KiB
Python
245 lines
8.1 KiB
Python
import random
|
|
import tkinter as tk
|
|
from tkinter import *
|
|
from tkinter.ttk import Combobox
|
|
from PIL import Image, ImageDraw
|
|
import os
|
|
|
|
promien = 5
|
|
punkty_do_zapisu = []
|
|
helped = False
|
|
|
|
|
|
def kolory():
|
|
lis = ["white", "white smoke", "gainsboro", "light gray", "silver", "gray", "dim gray", "black", "light slate gray",
|
|
"slate gray", "alice blue", "light steel blue", "cornflower blue", "royal blue", "blue", "medium blue",
|
|
"navy", "dark blue", "midnight blue", "light blue", "deep sky blue", "dodger blue", "powder blue",
|
|
"sky blue", "light sky blue", "steel blue", "azure", "light cyan", "cyan", "pale turquoise",
|
|
"dark turquoise", "turquoise", "medium turquoise", "light sea green", "cadet blue", "dark cyan", "teal",
|
|
"dark slate gray", "mint cream", "aquamarine", "medium aquamarine", "dark sea green", "medium sea green",
|
|
"sea green", "honeydew", "pale green", "light green", "medium spring green", "spring green", "lime green",
|
|
"green", "forest green", "dark green", "green yellow", "chartreuse", "lawn green", "lime", "yellow green",
|
|
"olive drab", "beige", "dark khaki", "olive", "dark olive green", "pale goldenrod", "khaki", "ivory",
|
|
"light yellow", "light goldenrod yellow", "cornsilk", "lemon chiffon", "yellow", "gold", "goldenrod",
|
|
"dark goldenrod", "wheat", "tan", "burlywood", "peru", "sienna", "saddle brown", "floral white", "old lace",
|
|
"navajo white", "moccasin", "sandy brown", "orange", "dark orange", "chocolate", "firebrick", "brown",
|
|
"dark red", "maroon", "antique white", "papaya whip", "blanched almond", "bisque", "peach puff",
|
|
"light salmon", "coral", "tomato", "orange red", "red", "crimson", "dark salmon", "salmon", "light coral",
|
|
"indian red", "rosy brown", "linen", "seashell", "misty rose", "pink", "light pink", "hot pink", "deep pink",
|
|
"snow", "lavender blush", "pale violet red", "violet red", "medium violet red", "purple", "dark magenta",
|
|
"violet", "magenta", "thistle", "plum", "orchid", "medium orchid", "dark orchid", "dark violet",
|
|
"blue violet", "medium purple", "rebecca purple", "indigo", "ghost white", "lavender", "light slate blue",
|
|
"medium slate blue", "slate blue", "dark slate blue"]
|
|
return lis[random.randrange(0, len(lis))]
|
|
|
|
|
|
def on_key_press(event):
|
|
print(f"Key pressed: {event.keysym}")
|
|
label = tk.Label(root, text=f"Key pressed: {event.keysym} ")
|
|
label.config(bg=kolory())
|
|
label.place(x=0, y=0)
|
|
if event.keysym == 'd':
|
|
canvas.move(kwadrat, 5, 0)
|
|
elif event.keysym == 'a':
|
|
canvas.move(kwadrat, -5, 0)
|
|
elif event.keysym == 'w':
|
|
canvas.move(kwadrat, 0, -5)
|
|
elif event.keysym == 's':
|
|
canvas.move(kwadrat, 0, 5)
|
|
# label.pack()
|
|
kolor()
|
|
|
|
|
|
def on_left_click(event):
|
|
print(f"Left click at ({event.x}, {event.y})")
|
|
global promien
|
|
aktualny_kolor = canvas.itemcget(podonzaj, "fill")
|
|
|
|
canvas.create_oval(event.x - promien, event.y - promien, event.x + promien, event.y + promien,
|
|
fill=aktualny_kolor, outline="")
|
|
punkty_do_zapisu.append((event.x, event.y, promien, aktualny_kolor))
|
|
|
|
|
|
def on_right_click(event):
|
|
print(f"Right click at ({event.x}, {event.y})")
|
|
|
|
|
|
def on_mouse_motion(event):
|
|
odswiez_pozycje_kropki(event.x, event.y)
|
|
|
|
|
|
def zapisz_obraz():
|
|
# 1. Znajdowanie wolnego numeru
|
|
licznik = 1
|
|
while os.path.exists(f"obraz_tk_{licznik}.png"):
|
|
licznik += 1
|
|
|
|
nazwa_pliku = f"obraz_tk_{licznik}.png"
|
|
|
|
# 2. Tworzenie i rysowanie obrazu (jak wcześniej)
|
|
szer = canvas.winfo_width()
|
|
wys = canvas.winfo_height()
|
|
obraz = Image.new("RGB", (szer, wys), "white")
|
|
rysuj = ImageDraw.Draw(obraz)
|
|
|
|
for x, y, r, kolor in punkty_do_zapisu:
|
|
rysuj.ellipse([x - r, y - r, x + r, y + r], fill=kolor)
|
|
|
|
# 3. Zapis pod nową nazwą
|
|
obraz.save(nazwa_pliku)
|
|
print(f"Zapisano jako {nazwa_pliku}")
|
|
|
|
|
|
def zmien_promien(wartosc):
|
|
global promien
|
|
promien = int(wartosc)
|
|
skrajny_x = root.winfo_pointerx()
|
|
skrajny_y = root.winfo_pointery()
|
|
canvas_x = skrajny_x - canvas.winfo_rootx()
|
|
canvas_y = skrajny_y - canvas.winfo_rooty()
|
|
odswiez_pozycje_kropki(canvas_x, canvas_y)
|
|
|
|
|
|
def odswiez_pozycje_kropki(x, y):
|
|
global promien
|
|
canvas.coords(podonzaj, x - promien, y - promien, x + promien, y + promien)
|
|
|
|
|
|
def relly():
|
|
rel = Button(root, text="Are you sure???", width=20, bg="red", command=root.destroy)
|
|
niet = Button(root, text="no", width=20, fg="black", command=lambda: [rel.place_forget(), niet.place_forget()])
|
|
rel.place(relx=0.5, rely=0.5, anchor=tk.CENTER)
|
|
niet.place(relx=1.0, rely=1.0, anchor='se', x=-10, y=-10)
|
|
root.bind("<Return>", lambda e: root.destroy)
|
|
root.bind("<Escape>", lambda e: [rel.place_forget(), niet.place_forget()])
|
|
# return 0
|
|
|
|
|
|
def skroluj_suwak(event):
|
|
obecna = vertical_scale.get()
|
|
|
|
if event.num == 4:
|
|
nowa = obecna + 1
|
|
elif event.num == 5:
|
|
nowa = obecna - 1
|
|
else:
|
|
nowa = obecna
|
|
|
|
vertical_scale.set(nowa)
|
|
|
|
|
|
def kolor(val=None):
|
|
r = red.get()
|
|
g = green.get()
|
|
b = blue.get()
|
|
|
|
kolor_hex = f"#{r:02x}{g:02x}{b:02x}"
|
|
canvas.itemconfig(podonzaj, fill=kolor_hex)
|
|
|
|
|
|
def r_up(event):
|
|
aktualna = red.get()
|
|
if aktualna < 255:
|
|
red.set(aktualna + 5)
|
|
|
|
|
|
def r_down(event):
|
|
aktualna = red.get()
|
|
if aktualna > 0:
|
|
red.set(aktualna - 5)
|
|
|
|
|
|
def g_up(event):
|
|
aktualna = green.get()
|
|
if aktualna < 255:
|
|
green.set(aktualna + 5)
|
|
|
|
|
|
def g_down(event):
|
|
aktualna = green.get()
|
|
if aktualna > 0:
|
|
green.set(aktualna - 5)
|
|
|
|
|
|
def b_up(event):
|
|
aktualna = blue.get()
|
|
if aktualna < 255:
|
|
blue.set(aktualna + 5)
|
|
|
|
|
|
def b_down(event):
|
|
aktualna = blue.get()
|
|
if aktualna > 0:
|
|
blue.set(aktualna - 5)
|
|
|
|
|
|
def helpp():
|
|
global helped
|
|
helped = not helped
|
|
if helped:
|
|
helplist.place(relx=1.0, y=60, anchor='ne')
|
|
else:
|
|
helplist.place_forget()
|
|
|
|
|
|
# global promien
|
|
root = Tk()
|
|
canvas = tk.Canvas(root, bg="white", highlightthickness=0)
|
|
canvas.pack(fill="both", expand=True)
|
|
kwadrat = canvas.create_rectangle(50, 50, 100, 100, fill="blue")
|
|
podonzaj = canvas.create_oval(100, 100, 110, 110, fill="green")
|
|
|
|
root.bind("<KeyPress>", on_key_press)
|
|
root.bind("<B1-Motion>", on_left_click)
|
|
root.bind("<Button-3>", on_right_click)
|
|
root.bind("<Motion>", on_mouse_motion)
|
|
root.bind_all("<Button-4>", skroluj_suwak)
|
|
root.bind_all("<Button-5>", skroluj_suwak)
|
|
|
|
root.bind("r", r_down)
|
|
root.bind("t", r_up)
|
|
root.bind("f", g_down)
|
|
root.bind("g", g_up)
|
|
root.bind("v", b_down)
|
|
root.bind("b", b_up)
|
|
|
|
stop = Button(root, text="Escape", width=20, bg="red", fg="black", command=relly)
|
|
stop.place(relx=0.5, rely=0, anchor='n', y=0)
|
|
root.bind("<Escape>", lambda e: relly())
|
|
|
|
przycisk_zapis = Button(root, text="Zapisz PNG", bg="green", fg="white", command=zapisz_obraz)
|
|
przycisk_zapis.place(relx=1.0, rely=0.0, anchor='ne')
|
|
|
|
vertical_scale = Scale(root, from_=1, to=50, command=zmien_promien)
|
|
vertical_scale.place(x=0, rely=0.5, anchor='sw')
|
|
vertical_scale.set(5)
|
|
|
|
blue = Scale(root, from_=0, to=255, command=kolor, orient=tk.HORIZONTAL)
|
|
blue.place(x=0, rely=1, anchor='sw')
|
|
blue.set(0)
|
|
green = Scale(root, from_=0, to=255, command=kolor, orient=tk.HORIZONTAL)
|
|
green.place(x=0, rely=1, anchor='sw', y=-40)
|
|
green.set(0)
|
|
red = Scale(root, from_=0, to=255, command=kolor, orient=tk.HORIZONTAL)
|
|
red.place(x=0, rely=1, anchor='sw', y=-80)
|
|
red.set(0)
|
|
rgb = Label(root, text="\nRed \n\n\nGreen \n\nBlue\n")
|
|
rgb.place(x=102, rely=1, anchor='nw', y=-122)
|
|
|
|
helpbuton = Button(root, text=" Help ", bg="lightblue", fg="blue", command=helpp)
|
|
helpbuton.place(relx=1.0, y=30, anchor='ne')
|
|
helplist = Listbox(root, bg="lightblue", fg="blue")
|
|
instrukcje = ["Scroll - size", "r - red-5", "t - red+5", "f - green-5", "g - green+5", "v - blue-5", "b - blue+5"]
|
|
|
|
for opcja in instrukcje:
|
|
helplist.insert(tk.END, opcja)
|
|
|
|
|
|
wszystkie_teksty = helplist.get(0, tk.END)
|
|
najdluzsza_dlugosc = 0
|
|
for tekst in wszystkie_teksty:
|
|
najdluzsza_dlugosc = max(najdluzsza_dlugosc, len(tekst))
|
|
helplist.config(width=12, height=helplist.size())
|
|
|
|
root.mainloop()
|