Dieses Mal werde ich einen Artikel über Tkinter schreiben.
Ich habe eine GUI erstellt, die ein Bild vertikal und horizontal in vier unterteilt und das geteilte Bild speichert.
Abgeschlossenes Bild
Dieses Mal wollte ich auf das Bild klicken und es speichern, damit es auf der Schaltfläche angezeigt wird Wechseln Sie zu einem anderen Bild im Ordner mit "Vorheriges Bild" und "Nächstes Bild" unten.
python
self.Button = tkinter.Button(master,width=sep,height=sep,text='Nicht ausgewählt') #Generieren Sie eine Schaltfläche.
self.Button["bd"] = "7"
self.Button["compound"] = "top"
self.Button["command"]=dvd_clicked
self.Button["image"]= self.image_tk
self.Button.grid(row=0, column=0)
Verbindung = Position der Zeichen, wenn das Bild platziert wird Befehl = Ereignis, wenn die Taste gedrückt wird image = Bild, das angezeigt werden soll
def dvd_clicked():
self.Button["text"] = "Wahl"
self.Button["fg"] = "RED"
self.im_crop = self.image_pil.crop((self.Clp[0],self.Clp[0],self.Clp[1],self.Clp[1]))
self.im_crop.save('moji/Clp'+str(basename_without[i])+'0.jpg', quality=95)
Hier wird das Bild gespeichert, wenn auf jede Schaltfläche geklickt wird. Um jedes Teil zu speichern, habe ich Standortinformationen wie 0_1 hinzugefügt und die Datei gespeichert.
Nach Auswahl des ausgewählten Bereichs wird der Text in Rot "ausgewählt".
Wenn Sie auf "Vorheriges Bild" oder "Nächstes Bild" klicken, wird jedes Bild aktualisiert.
import tkinter as tk
import tkinter
import cv2
from PIL import Image, ImageTk
import os
import glob
os.chdir('./')
class Application(tk.Frame):
def __init__(self,master):
super().__init__(master)
master.title("Basisfenster")
global i
self.window = []
self.user = []
self.seq=0
i=0
self.size=64 #Geteilte Größe
sep = 70#Bestimmung der Tastengröße
self.Clp=[0 ,self.size*1,self.size*2,self.size*3,self.size*4]
def dvd_clicked():
self.Button["text"] = "Wahl"
self.Button["fg"] = "RED"
self.im_crop = self.image_pil.crop((self.Clp[0],self.Clp[0],self.Clp[1],self.Clp[1]))
self.im_crop.save('moji/Clp'+str(basename_without[i])+'0.jpg', quality=95)
def dvd_clicked0_1():
self.Button0_1["text"] = "Wahl"
self.Button0_1["fg"] = "RED"
self.im_crop = self.image_pil.crop((self.Clp[1],self.Clp[0],self.Clp[2],self.Clp[1]))
self.im_crop.save('moji/Clp'+str(basename_without[i])+'0_1.jpg', quality=95)
def dvd_clicked0_2():
self.Button0_2["text"] = "Wahl"
self.Button0_2["fg"] = "RED"
self.im_crop = self.image_pil.crop((self.Clp[2],self.Clp[0],self.Clp[3],self.Clp[1]))
self.im_crop.save('moji/Clp'+str(basename_without[i])+'0_2.jpg', quality=95)
def dvd_clicked0_3():
self.Button0_3["text"] = "Wahl"
self.Button0_3["fg"] = "RED"
self.im_crop = self.image_pil.crop((192, 0, 256, 64))
self.im_crop.save('moji/Clp'+str(basename_without[i])+'0_3.jpg', quality=95)
def dvd_clicked1():
self.Button1["text"] = "Wahl"
self.Button1["fg"] = "RED"
self.im_crop = self.image_pil.crop((0, 64, 64, 128))
self.im_crop.save('moji/Clp'+str(basename_without[i])+'1.jpg', quality=95)
def dvd_clicked1_1():
self.Button1_1["text"] = "Wahl"
self.Button1_1["fg"] = "RED"
self.im_crop = self.image_pil.crop((64, 64, 128, 128))
self.im_crop.save('moji/Clp'+str(basename_without[i])+'1_1.jpg', quality=95)
def dvd_clicked1_2():
self.Button1_2["text"] = "Wahl"
self.Button1_2["fg"] = "RED"
self.im_crop = self.image_pil.crop((128, 64, 192, 128))
self.im_crop.save('moji/Clp'+str(basename_without[i])+'1_2.jpg', quality=95)
def dvd_clicked1_3():
self.Button1_3["text"] = "Wahl"
self.Button1_3["fg"] = "RED"
self.im_crop = self.image_pil.crop((192, 64, 256, 128))
self.im_crop.save('moji/Clp'+str(basename_without[i])+'1_3.jpg', quality=95)
def dvd_clicked2():
self.Button2["text"] = "Wahl"
self.Button2["fg"] = "RED"
self.im_crop = self.image_pil.crop((0, 128, 64, 192))
self.im_crop.save('moji/Clp'+str(basename_without[i])+'2.jpg', quality=95)
def dvd_clicked2_1():
self.Button2_1["text"] = "Wahl"
self.Button2_1["fg"] = "RED"
self.im_crop = self.image_pil.crop((64, 128, 128, 192))
self.im_crop.save('moji/Clp'+str(basename_without[i])+'2_1.jpg', quality=95)
def dvd_clicked2_2():
self.Button2_2["text"] = "Wahl"
self.Button2_2["fg"] = "RED"
self.im_crop = self.image_pil.crop((128, 128, 192, 192))
self.im_crop.save('moji/Clp'+str(basename_without[i])+'2_2.jpg', quality=95)
def dvd_clicked2_3():
self.Button2_3["text"] = "Wahl"
self.Button2_3["fg"] = "RED"
self.im_crop = self.image_pil.crop((192, 128, 256, 192))
self.im_crop.save('moji/Clp'+str(basename_without[i])+'2_3.jpg', quality=95)
def dvd_clicked3():
self.Button3["text"] = "Wahl"
self.Button3["fg"] = "RED"
self.im_crop = self.image_pil.crop((0, 192, 64, 256))
self.im_crop.save('moji/Clp'+str(basename_without[i])+'3.jpg', quality=95)
def dvd_clicked3_1():
self.Button3_1["text"] = "Wahl"
self.Button3_1["fg"] = "RED"
self.im_crop = self.image_pil.crop((64, 192, 128, 256))
self.im_crop.save('moji/Clp'+str(basename_without[i])+'3_1.jpg', quality=95)
def dvd_clicked3_2():
self.Button3_2["text"] = "Wahl"
self.Button3_2["fg"] = "RED"
self.im_crop = self.image_pil.crop((128, 192, 192, 256))
self.im_crop.save('moji/Clp'+str(basename_without[i])+'3_2.jpg', quality=95)
def dvd_clicked3_3():
self.Button3_3["text"] = "Wahl"
self.Button3_3["fg"] = "RED"
self.im_crop = self.image_pil.crop((192, 192, 256, 256))
self.im_crop.save('moji/Clp'+str(basename_without[i])+'3_3.jpg', quality=95)
def dvd():
global i
"""Generierung von Dateinamen"""
i+=1
if(i>=len(filename)):
i=0
self.Button.grid_remove()
#filename[i] = "tihou.jpg "
image_bgr = cv2.imread(filename[i])
image_bgr = cv2.resize(image_bgr , (256, 256))
image_rgb = cv2.cvtColor(image_bgr, cv2.COLOR_BGR2RGB) #Da imread BGR ist, wird es in RGB konvertiert
self.image_pil = Image.fromarray(image_rgb) #Konvertieren Sie vom RGB- in das PIL-Format
self.im_crop = self.image_pil.crop((self.Clp[0],self.Clp[0],self.Clp[1],self.Clp[1]))
self.image_tk = ImageTk.PhotoImage(self.im_crop) #In das ImageTk-Format konvertieren
self.im_crop = self.image_pil.crop((self.Clp[1],self.Clp[0],self.Clp[2],self.Clp[1]))
self.image_tk0_1 = ImageTk.PhotoImage(self.im_crop) #In das ImageTk-Format konvertieren
self.im_crop = self.image_pil.crop((self.Clp[2],self.Clp[0],self.Clp[3],self.Clp[1]))
self.image_tk0_2 = ImageTk.PhotoImage(self.im_crop) #In das ImageTk-Format konvertieren
self.im_crop = self.image_pil.crop((self.Clp[3],self.Clp[0],self.Clp[4],self.Clp[1]))
self.image_tk0_3 = ImageTk.PhotoImage(self.im_crop) #In das ImageTk-Format konvertieren
self.im_crop = self.image_pil.crop((self.Clp[0],self.Clp[1],self.Clp[1],self.Clp[2]))
self.image_tk1 = ImageTk.PhotoImage(self.im_crop) #In das ImageTk-Format konvertieren
self.im_crop = self.image_pil.crop((self.Clp[1],self.Clp[1],self.Clp[2],self.Clp[2]))
self.image_tk1_1 = ImageTk.PhotoImage(self.im_crop) #In das ImageTk-Format konvertieren
self.im_crop = self.image_pil.crop((self.Clp[2],self.Clp[1],self.Clp[3],self.Clp[2]))
self.image_tk1_2 = ImageTk.PhotoImage(self.im_crop) #In das ImageTk-Format konvertieren
self.im_crop = self.image_pil.crop((self.Clp[3],self.Clp[1],self.Clp[4],self.Clp[2]))
self.image_tk1_3 = ImageTk.PhotoImage(self.im_crop) #In das ImageTk-Format konvertieren
self.im_crop = self.image_pil.crop((self.Clp[0],self.Clp[2],self.Clp[1],self.Clp[3]))
self.image_tk2 = ImageTk.PhotoImage(self.im_crop) #In das ImageTk-Format konvertieren
self.im_crop = self.image_pil.crop((self.Clp[1],self.Clp[2],self.Clp[2],self.Clp[3]))
self.image_tk2_1 = ImageTk.PhotoImage(self.im_crop) #In das ImageTk-Format konvertieren
self.im_crop = self.image_pil.crop((self.Clp[2],self.Clp[2],self.Clp[3],self.Clp[3]))
self.image_tk2_2 = ImageTk.PhotoImage(self.im_crop) #In das ImageTk-Format konvertieren
self.im_crop = self.image_pil.crop((self.Clp[3],self.Clp[2],self.Clp[4],self.Clp[3]))
self.image_tk2_3 = ImageTk.PhotoImage(self.im_crop) #In das ImageTk-Format konvertieren
self.im_crop = self.image_pil.crop((self.Clp[0],self.Clp[3],self.Clp[1],self.Clp[4]))
self.image_tk3 = ImageTk.PhotoImage(self.im_crop) #In das ImageTk-Format konvertieren
self.im_crop = self.image_pil.crop((self.Clp[1],self.Clp[3],self.Clp[2],self.Clp[4]))
self.image_tk3_1 = ImageTk.PhotoImage(self.im_crop) #In das ImageTk-Format konvertieren
self.im_crop = self.image_pil.crop((self.Clp[2],self.Clp[3],self.Clp[3],self.Clp[4]))
self.image_tk3_2 = ImageTk.PhotoImage(self.im_crop) #In das ImageTk-Format konvertieren
self.im_crop = self.image_pil.crop((self.Clp[3],self.Clp[3],self.Clp[4],self.Clp[4]))
self.image_tk3_3 = ImageTk.PhotoImage(self.im_crop) #In das ImageTk-Format konvertieren
#Aktualisieren Sie die Schaltfläche.
self.Button["image"]= self.image_tk
self.Button0_1["image"]= self.image_tk0_1
self.Button0_2["image"]= self.image_tk0_2
self.Button0_3["image"]= self.image_tk0_3
#Aktualisieren Sie die Schaltfläche.
self.Button1["image"]= self.image_tk1
self.Button1_1["image"]= self.image_tk1_1
self.Button1_2["image"]= self.image_tk1_2
self.Button1_3["image"]= self.image_tk1_3
#Aktualisieren Sie die Schaltfläche.
self.Button2["image"]= self.image_tk2
self.Button2_1["image"]= self.image_tk2_1
self.Button2_2["image"]= self.image_tk2_2
self.Button2_3["image"]= self.image_tk2_3
#Aktualisieren Sie die Schaltfläche.
self.Button3["image"]= self.image_tk3
self.Button3.grid(row=0, column=0)
self.Button3_1["image"]= self.image_tk3_1
self.Button3_2["image"]= self.image_tk3_2
self.Button3_3["image"]= self.image_tk3_3
self.Button.grid(row=0, column=0)
self.Button0_1.grid(row=0, column=1)
self.Button0_2.grid(row=0, column=2)
self.Button0_3.grid(row=0, column=3)
self.Button1.grid(row=1, column=0)
self.Button1_1.grid(row=1, column=1)
self.Button1_2.grid(row=1, column=2)
self.Button1_3.grid(row=1, column=3)
self.Button2.grid(row=2, column=0)
self.Button2_1.grid(row=2, column=1)
self.Button2_2.grid(row=2, column=2)
self.Button2_3.grid(row=2, column=3)
self.Button3.grid(row=3, column=0)
self.Button3_1.grid(row=3, column=1)
self.Button3_2.grid(row=3, column=2)
self.Button3_3.grid(row=3, column=3)
basename = []
basename_without=[]
filename =glob.glob('*.jpg')
for x in range(len(filename)):
#Dateiname(Mit Verlängerung)Zum Laden von Bildern
basename1 = os.path.basename(filename[x])
basename.append(basename1)
#Dateiname(Keine Verlängerung)Wird beim Speichern von Bildern verwendet
basename_without_ext = os.path.splitext(os.path.basename(filename[x]))[0]
basename_without.append(basename_without_ext)
#filename[i] = "tihou.jpg "
image_bgr = cv2.imread(filename[i])
image_bgr = cv2.resize(image_bgr , (256, 256))
image_rgb = cv2.cvtColor(image_bgr, cv2.COLOR_BGR2RGB) #Da imread BGR ist, wird es in RGB konvertiert
self.image_pil = Image.fromarray(image_rgb) #Konvertieren Sie vom RGB- in das PIL-Format
self.im_crop = self.image_pil.crop((self.Clp[0],self.Clp[0],self.Clp[1],self.Clp[1]))
self.image_tk = ImageTk.PhotoImage(self.im_crop) #In das ImageTk-Format konvertieren
#Platziere den Knopf.
self.Button = tkinter.Button(master,width=sep,height=sep,text='Nicht ausgewählt') #Generieren Sie eine Schaltfläche.
self.Button["bd"] = "7"
self.Button["compound"] = "top"
self.Button["command"]=dvd_clicked
self.Button["image"]= self.image_tk
self.Button.grid(row=0, column=0)
self.im_crop = self.image_pil.crop((self.Clp[1],self.Clp[0],self.Clp[2],self.Clp[1]))
self.image_tk0_1 = ImageTk.PhotoImage(self.im_crop) #In das ImageTk-Format konvertieren
self.Button0_1= tkinter.Button(master,width=sep,height=sep,text='Nicht ausgewählt',image = self.image_tk0_1) #Generieren Sie eine Schaltfläche.
self.Button0_1["bd"] = "7"
self.Button0_1["compound"] = "top"
self.Button0_1["command"]=dvd_clicked0_1
self.Button0_1.grid(row=0, column=1)
self.im_crop = self.image_pil.crop((self.Clp[2],self.Clp[0],self.Clp[3],self.Clp[1]))
self.image_tk0_2 = ImageTk.PhotoImage(self.im_crop) #In das ImageTk-Format konvertieren
self.Button0_2= tkinter.Button(master,width=sep,height=sep,text='Nicht ausgewählt',image = self.image_tk0_2) #Generieren Sie eine Schaltfläche.
self.Button0_2["bd"] = "7"
self.Button0_2["compound"] = "top"
self.Button0_2["command"]=dvd_clicked0_2
self.Button0_2.grid(row=0, column=2)
canvas0_3 = tkinter.Canvas(master, width=sep, height=sep) #Leinwanderstellung
#canvas.pack()
self.im_crop = self.image_pil.crop((192, 0, 256, 64))
self.image_tk0_3 = ImageTk.PhotoImage(self.im_crop) #In das ImageTk-Format konvertieren
self.Button0_3= tkinter.Button(master,width=sep,height=sep,text='Nicht ausgewählt',image = self.image_tk0_3) #Generieren Sie eine Schaltfläche.
self.Button0_3["bd"] = "7"
self.Button0_3["compound"] = "top"
self.Button0_3["command"]=dvd_clicked0_3
self.Button0_3.grid(row=0, column=3)
self.im_crop = self.image_pil.crop((0, 64, 64, 128))
self.image_tk1 = ImageTk.PhotoImage(self.im_crop) #In das ImageTk-Format konvertieren
self.Button1= tkinter.Button(master,width=sep,height=sep,text='Nicht ausgewählt' ,image = self.image_tk1) #Generieren Sie eine Schaltfläche.
self.Button1["bd"] = "7"
self.Button1["compound"] = "top"
self.Button1["command"]=dvd_clicked1
self.Button1.grid(row=1, column=0)
self.im_crop = self.image_pil.crop((64, 64, 128, 128))
self.image_tk1_1 = ImageTk.PhotoImage(self.im_crop) #In das ImageTk-Format konvertieren
self.Button1_1= tkinter.Button(master,width=sep,height=sep,text='Nicht ausgewählt' ,image = self.image_tk1_1) #Generieren Sie eine Schaltfläche.
self.Button1_1["bd"] = "7"
self.Button1_1["compound"] = "top"
self.Button1_1["command"]=dvd_clicked1_1
self.Button1_1.grid(row=1, column=1)
self.im_crop = self.image_pil.crop((128, 64, 192, 128))
self.image_tk1_2 = ImageTk.PhotoImage(self.im_crop) #In das ImageTk-Format konvertieren
self.Button1_2= tkinter.Button(master,width=sep,height=sep,text='Nicht ausgewählt' ,image = self.image_tk1_2) #Generieren Sie eine Schaltfläche.
self.Button1_2["bd"] = "7"
self.Button1_2["compound"] = "top"
self.Button1_2["command"]=dvd_clicked1_2
self.Button1_2.grid(row=1, column=2)
canvas1_3 = tkinter.Canvas(master, width=sep, height=sep) #Leinwanderstellung
#canvas.pack()
self.im_crop = self.image_pil.crop((192 ,64, 256, 128))
self.image_tk1_3 = ImageTk.PhotoImage(self.im_crop) #In das ImageTk-Format konvertieren
self.Button1_3= tkinter.Button(master,width=sep,height=sep,text='Nicht ausgewählt',image = self.image_tk1_3) #Generieren Sie eine Schaltfläche.
self.Button1_3["bd"] = "7"
self.Button1_3["compound"] = "top"
self.Button1_3["command"]=dvd_clicked1_3
self.Button1_3.grid(row=1, column=3)
self.im_crop = self.image_pil.crop((0, 128, 64, 192))
self.image_tk2 = ImageTk.PhotoImage(self.im_crop) #In das ImageTk-Format konvertieren
self.Button2= tkinter.Button(master,width=sep,height=sep,text='Nicht ausgewählt' ,image = self.image_tk2) #Generieren Sie eine Schaltfläche.
self.Button2["bd"] = "7"
self.Button2["compound"] = "top"
self.Button2["command"]=dvd_clicked2
self.Button2.grid(row=2, column=0)
self.im_crop = self.image_pil.crop((64, 128, 128, 192))
self.image_tk2_1 = ImageTk.PhotoImage(self.im_crop) #In das ImageTk-Format konvertieren
self.Button2_1= tkinter.Button(master,width=sep,height=sep,text='Nicht ausgewählt' ,image = self.image_tk2_1) #Generieren Sie eine Schaltfläche.
self.Button2_1["bd"] = "7"
self.Button2_1["compound"] = "top"
self.Button2_1["command"]=dvd_clicked2_1
self.Button2_1.grid(row=2, column=1)
self.im_crop = self.image_pil.crop((128, 128, 192, 192))
self.image_tk2_2 = ImageTk.PhotoImage(self.im_crop) #In das ImageTk-Format konvertieren
self.Button2_2= tkinter.Button(master,width=sep,height=sep,text='Nicht ausgewählt' ,image = self.image_tk2_2) #Generieren Sie eine Schaltfläche.
self.Button2_2["bd"] = "7"
self.Button2_2["compound"] = "top"
self.Button2_2["command"]=dvd_clicked2_2
self.Button2_2.grid(row=2, column=2)
self.im_crop = self.image_pil.crop((192, 128, 256, 192))
self.image_tk2_3 = ImageTk.PhotoImage(self.im_crop) #In das ImageTk-Format konvertieren
self.Button2_3= tkinter.Button(master,width=sep,height=sep,text='Nicht ausgewählt',image = self.image_tk2_3) #Generieren Sie eine Schaltfläche.
self.Button2_3["bd"] = "7"
self.Button2_3["compound"] = "top"
self.Button2_3["command"]=dvd_clicked2_3
self.Button2_3.grid(row=2, column=3)
self.im_crop = self.image_pil.crop((0, 192, 64, 256))
self.image_tk3 = ImageTk.PhotoImage(self.im_crop) #In das ImageTk-Format konvertieren
self.Button3= tkinter.Button(master,width=sep,height=sep,text='Nicht ausgewählt',image = self.image_tk3) #Generieren Sie eine Schaltfläche.
self.Button3["bd"] = "7"
self.Button3["compound"] = "top"
self.Button3["command"]=dvd_clicked3
self.Button3.grid(row=3, column=0)
self.im_crop = self.image_pil.crop((64, 192, 128, 256))
self.image_tk3_1 = ImageTk.PhotoImage(self.im_crop) #In das ImageTk-Format konvertieren
self.Button3_1= tkinter.Button(master,width=sep,height=sep,text='Nicht ausgewählt' ,image = self.image_tk3_1) #Generieren Sie eine Schaltfläche.
self.Button3_1["bd"] = "7"
self.Button3_1["compound"] = "top"
self.Button3_1["command"]=dvd_clicked3_1
self.Button3_1.grid(row=3, column=1)
self.im_crop = self.image_pil.crop((128, 192, 192, 256))
self.image_tk3_2 = ImageTk.PhotoImage(self.im_crop) #In das ImageTk-Format konvertieren
self.Button3_2= tkinter.Button(master,width=sep,height=sep,text='Nicht ausgewählt' ,image = self.image_tk3_2) #Generieren Sie eine Schaltfläche.
self.Button3_2["bd"] = "7"
self.Button3_2["compound"] = "top"
self.Button3_2["command"]=dvd_clicked3_2
self.Button3_2.grid(row=3, column=2)
self.im_crop = self.image_pil.crop((192, 192, 256, 256))
self.image_tk3_3 = ImageTk.PhotoImage(self.im_crop) #In das ImageTk-Format konvertieren
self.Button3_3= tkinter.Button(master,width=sep,height=sep,text='Nicht ausgewählt' ,image = self.image_tk3_3) #Generieren Sie eine Schaltfläche.
self.Button3_3["bd"] = "7"
self.Button3_3["compound"] = "top"
self.Button3_3["command"]=dvd_clicked3_3
self.Button3_3.grid(row=3, column=3)
#Platzieren Sie das Etikett.
label1 = tkinter.Label(master,text='Bitte wählen Sie einen Lagerbereich')
label1["font"] = ("",18)
label1["height"] = 2
label1.grid(row=4, column=0,columnspan=4)
self.SButton1= tkinter.Button(master,text='<<Vorheriges Bild' ) #Generieren Sie eine Schaltfläche.
self.SButton1["bd"] = "7"
self.SButton1.grid(row=5, column=0,columnspan=2)
self.SButton2= tkinter.Button(master,text='Nächstes Bild>>' ) #Generieren Sie eine Schaltfläche.
self.SButton2["bd"] = "7"
self.SButton2["command"]=dvd
self.SButton2.grid(row=5, column=2,columnspan=2)
def main():
win = tk.Tk()
app = Application(win)
app.mainloop()
if __name__ == '__main__':
main()
Recommended Posts