Python’da Paint uygulaması #34 tkinter PIL
Python’da Paint uygulaması #34 tkinter PIL. Pythond’da tkinter ve PIL kütüphaneleri kullanarak bir Paint uygulması hazırlıyoruz. Renk seçimi, fırça büyklüğü değiştirme, erkan temizleme ve kaydetme bölümleri bulunmaktadır.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
from tkinter import * from tkinter import colorchooser, filedialog from PIL import ImageGrab def paint(event): global kalemboyu kalemboyu = sli.get() x1,y1,x2,y2 = (event.x-kalemboyu), (event.y-kalemboyu), (event.x+kalemboyu), (event.y+kalemboyu) canvas2.create_oval(x1,y1,x2,y2, fill=kalemrengi, outline=kalemrengi) def renkdegis(yeni_renk): global kalemrengi kalemrengi = yeni_renk def kalemrengisec(): global kalemrengi yeni_kalemrengi=colorchooser.askcolor(initialcolor='#00ff00', title='Renk secin')[1] kalemrengi = yeni_kalemrengi def arkaalanrengisec(): yeni_arkaalanrengi=colorchooser.askcolor(initialcolor='#00ff00', title='Renk secin')[1] canvas2.configure(bg=yeni_arkaalanrengi) def renk_kutulari(): id = canvas.create_rectangle((10,10,30,30), fill='black') canvas.tag_bind(id, '<Button-1>', lambda x: renkdegis('black')) id = canvas.create_rectangle((40,10,60,30), fill='gray') canvas.tag_bind(id, '<Button-1>', lambda x: renkdegis('gray')) id = canvas.create_rectangle((70,10,90,30), fill='brown4') canvas.tag_bind(id, '<Button-1>', lambda x: renkdegis('brown4')) id = canvas.create_rectangle((100,10,120,30), fill='red') canvas.tag_bind(id, '<Button-1>', lambda x: renkdegis('red')) id = canvas.create_rectangle((130,10,150,30), fill='orange') canvas.tag_bind(id, '<Button-1>', lambda x: renkdegis('orange')) id = canvas.create_rectangle((160,10,180,30), fill='yellow') canvas.tag_bind(id, '<Button-1>', lambda x: renkdegis('yellow')) id = canvas.create_rectangle((190,10,210,30), fill='green') canvas.tag_bind(id, '<Button-1>', lambda x: renkdegis('green')) id = canvas.create_rectangle((220,10,240,30), fill='blue') canvas.tag_bind(id, '<Button-1>', lambda x: renkdegis('blue')) id = canvas.create_rectangle((250,10,270,30), fill='purple') canvas.tag_bind(id, '<Button-1>', lambda x: renkdegis('purple')) def temizle(): canvas2.delete(ALL) canvas2.configure(bg='white') def kaydet(): file = filedialog.asksaveasfilename(filetypes=[('JPG','*.jpg')], defaultextension='jpg') ImageGrab.grab(bbox=(x_coordinate+130,y_coordinate+135, x_coordinate+pencere_width+280, y_coordinate+pencere_height+180)).save(file) pencere = Tk() pencere.title('Paint uygulaması') pencere_width = 800 pencere_height = 650 screen_width = pencere.winfo_screenwidth() screen_height = pencere.winfo_screenheight() x_coordinate = int(screen_width/2-pencere_width/2) y_coordinate = int(screen_height/2-pencere_height/2) pencere.geometry('{}x{}+{}+{}'.format(pencere_width, pencere_height,x_coordinate, y_coordinate)) kalemrengi = 'black' kalemboyu = 5 canvas = Canvas(pencere, width=750, height=50) canvas.pack() canvas2 = Canvas(pencere, width=750, height=550, background='white') canvas2.pack() canvas2.bind('<B1-Motion>', paint) Button(pencere, text='Temizle', command=temizle).place(x=300, y=15) Button(pencere, text='Kaydet', command=kaydet).place(x=360, y=15) sli = Scale(pencere, from_=1, to=20, orient=HORIZONTAL) sli.place(x=410, y=15) sli.set(kalemboyu) Button(pencere, text='Kalem rengi', command=kalemrengisec).place(x=530, y=15) Button(pencere, text='Arka alan rengi', command=arkaalanrengisec).place(x=610, y=15) renk_kutulari() mainloop() |