GUI: erzeugte BIlder auf Buttons durch weiteren Buttonklick rückgängig machen?
Verfasst: Donnerstag 11. Januar 2018, 20:41
Hallo!
ich arbeite an einem Schulprojekt und soll etwas kreatives mit tkinter erstellen. Ich hab an eine Art Bildergeschichte gedacht.. jetzt habe ich links 12 kleine Buttons,wenn man die klickt erscheinen auf den 12 Buttons rechts Bilder mit Unterschriften (die Bildergeschichte halt), hätte aber gerne, dass man die Geschichte beliebig oft wiederholen kann.. ich habe bereits einen "repeat"-Button, weiß jedoch nicht wie der command dazu programiert werden soll, hab schon alles mögliche ausprobiert aber der rest des programms funktioniert...
from tkinter import *
#Kommandos
def wechsleBild1():
b1["image"]=img1
lab1 = Label(root,text="ein kleines,\n einsames Kätzchen.\n", fg = "black",
font = "Helvetica 12 bold italic").place(x=205, y=225, width=175, height=70)
return
def wechsleBild2():
b2["image"]=img2
lab2 = Label(root,text="Ihr größter Wunsch\n war es endlich\n einen Freund zum\n Spielen zu finden.\n", fg = "black",
font = "Helvetica 12 bold italic").place(x=395, y=240, width=175, height=85)
return
def wechsleBild3():
b3["image"]=img3
lab3 = Label(root,text="Also begab sie\n sich auf eine Mission!\n", fg = "black",
font = "Helvetica 12 bold italic").place(x=590, y=230, width=175, height=75)
return
def wechsleBild4():
b4["image"]=img4
lab4 = Label(root,text="Auf ihrer Reise\n lagen viele\n Hindernisse,\n gefährliche Anhöhen..\n", fg = "black",
font = "Helvetica 12 bold italic").place(x=795, y=240, width=175, height=85)
return
def wechsleBild5():
b5["image"]=img5
lab5 = Label(root,text="und ewig lange\n Höhlenwege.\n", fg = "black",
font = "Helvetica 12 bold italic").place(x=995, y=235, width=175, height=70)
return
def wechsleBild6():
b6["image"]=img6
lab6 = Label(root,text="Und dann traf sie IHN,\n Onkel Barsik!\n", fg = "black",
font = "Helvetica 12 bold italic").place(x=1190, y=230, width=175, height=85)
return
def wechsleBild7():
b7["image"]=img7
lab7 = Label(root,text="Wie sie diesen alten\n Kater doch hasste!\n Oft hatte sie sich\n schon die schlimmsten\n Szenarien seines\n Todes vorgestellt.\n",
fg = "black", font = "Helvetica 12 bold italic").place(x=205, y=590, width=175, height=120)
return
def wechsleBild8():
b8["image"]=img8
lab8 = Label(root,text="Doch sie war\n vernünftig und\n schlich leise weiter.\n", fg = "black",
font = "Helvetica 12 bold italic").place(x=400, y=585, width=175, height=80)
return
def wechsleBild9():
b9["image"]=img9
lab9 = Label(root,text="Zwischendurch war\n natürlich ein\n Schläfchen und eine\n neue Frisur fällig.\n", fg = "black",
font = "Helvetica 12 bold italic").place(x=600, y=595, width=175, height=85)
return
def wechsleBild10():
b10["image"]=img10
lab10 = Label(root,text="Und was für\n neue Talente sie\n für sich entdeckte!\n", fg = "black",
font = "Helvetica 12 bold italic").place(x=800, y=585, width=175, height=80)
return
def wechsleBild11():
b11["image"]=img11
lab11 = Label(root,text="Dann endlich...\n Auf den ersten Blick\n erkannte sie für sich\n den Freund fürs Leben\n in einem Kücken!\n",
fg = "black", font = "Helvetica 12 bold italic").place(x=1000, y=590, width=175, height=105)
return
def wechsleBild12():
b12["image"]=img12
lab12 = Label(root,text="Heute noch erinnert\n sie sich gerne an\n dieses Abendteuer\n und ihren alten\n Freund das Kücken...\n", fg = "black",
font = "Helvetica 12 bold italic").place(x=1200, y=590, width=175, height=105)
return
def repeat():
command["wechsleBild12"]= b12
command["wechsleBild11"] = b11
command["wechsleBild10"] = b10
command["wechsleBild9"] = b9
command["wechsleBild8"] = b8
command["wechsleBild7"] = b7
command["wechsleBild6"] = b6
command["wechsleBild5"] = b5
command["wechsleBild4"] = b4
command["wechsleBild3"]= b3
command["wechsleBild2"] = b2
command["wechsleBild1"] = b1
return
root = Tk()
#Überschrift
root.title("Meine GUI - Bildergeschichte ")
#Bilder einfügen
img1 = PhotoImage(file="E:\INFO\GUI\Bilder2\Katze1.gif")
img2 = PhotoImage(file="E:\INFO\GUI\Bilder2\Katze2.gif")
img3 = PhotoImage(file="E:\INFO\GUI\Bilder2\Katze3.gif")
img4 = PhotoImage(file="E:\INFO\GUI\Bilder2\Katze4.gif")
img5 = PhotoImage(file="E:\INFO\GUI\Bilder2\Katze5.gif")
img6 = PhotoImage(file="E:\INFO\GUI\Bilder2\Katze6.gif")
img7 = PhotoImage(file="E:\INFO\GUI\Bilder2\Katze7.gif")
img8 = PhotoImage(file="E:\INFO\GUI\Bilder2\Katze8.gif")
img9 = PhotoImage(file="E:\INFO\GUI\Bilder2\Katze9.gif")
img10 = PhotoImage(file="E:\INFO\GUI\Bilder2\Katze10.gif")
img11 = PhotoImage(file="E:\INFO\GUI\Bilder2\Katze11.gif")
img12 = PhotoImage(file="E:\INFO\GUI\Bilder2\Katze12.gif")
#Label oben setzen
lab0 = Label(root, text="Es war einmal...",
fg = "black",
font = "Helvetica 15 bold italic").pack(side=TOP)
#Knöpfe erstellen
b01 = Button(root, text="1", command=wechsleBild1, fg="green")
b02 = Button(root, text="2", command=wechsleBild2, fg="green")
b03 = Button(root, text="3", command=wechsleBild3, fg="green")
b04 = Button(root, text="4", command=wechsleBild4, fg="green")
b05 = Button(root, text="5", command=wechsleBild5, fg="green")
b06 = Button(root, text="6", command=wechsleBild6, fg="green")
b07 = Button(root, text="7", command=wechsleBild7, fg="green")
b08 = Button(root, text="8", command=wechsleBild8, fg="green")
b09 = Button(root, text="9", command=wechsleBild9, fg="green")
b010 = Button(root, text="10", command=wechsleBild10, fg="green")
b011 = Button(root, text="11", command=wechsleBild11, fg="green")
b012 = Button(root, text="12", command=wechsleBild12, fg="green")
b013 = Button(root, text="repeat", command=repeat, fg="green")
b1 = Button(root, text="*Bild*", fg="green")
b2 = Button(root, text="*Bild*", fg="green")
b3 = Button(root, text="*Bild*", fg="green")
b4 = Button(root, text="*Bild*", fg="green")
b5 = Button(root, text="*Bild*", fg="green")
b6 = Button(root, text="*Bild*", fg="green")
b7 = Button(root, text="*Bild*", fg="green")
b8 = Button(root, text="*Bild*", fg="green")
b9 = Button(root, text="*Bild*", fg="green")
b10 = Button(root, text="*Bild*", fg="green")
b11 = Button(root, text="*Bild*", fg="green")
b12 = Button(root, text="*Bild*", fg="green")
#Knöpfe plazieren
b01.place(x=25, y=50, width=50, height=50)
b02.place(x=25, y=100, width=50, height=50)
b03.place(x=25, y=150, width=50, height=50)
b04.place(x=25, y=200, width=50, height=50)
b05.place(x=25, y=250, width=50, height=50)
b06.place(x=25, y=300, width=50, height=50)
b07.place(x=25, y=350, width=50, height=50)
b08.place(x=25, y=400, width=50, height=50)
b09.place(x=25, y=450, width=50, height=50)
b010.place(x=25, y=500, width=50, height=50)
b011.place(x=25, y=550, width=50, height=50)
b012.place(x=25, y=600, width=50, height=50)
b013.place(x=25, y=650, width=50, height=50)
b1.place(x=200, y=50, width=175, height=175)
b2.place(x=400, y=50, width=175, height=175)
b3.place(x=600, y=50,width=175, height=175)
b4.place(x=800, y=50, width=175, height=175)
b5.place(x=1000, y=50, width=175, height=175)
b6.place(x=1200, y=50, width=175, height=175)
b7.place(x=200, y=400, width=175, height=175)
b8.place(x=400, y=400, width=175, height=175)
b9.place(x=600, y=400, width=175, height=175)
b10.place(x=800, y=400, width=175, height=175)
b11.place(x=1000, y=400, width=175, height=175)
b12.place(x=1200, y=400, width=175, height=175)
root.mainloop()
ich arbeite an einem Schulprojekt und soll etwas kreatives mit tkinter erstellen. Ich hab an eine Art Bildergeschichte gedacht.. jetzt habe ich links 12 kleine Buttons,wenn man die klickt erscheinen auf den 12 Buttons rechts Bilder mit Unterschriften (die Bildergeschichte halt), hätte aber gerne, dass man die Geschichte beliebig oft wiederholen kann.. ich habe bereits einen "repeat"-Button, weiß jedoch nicht wie der command dazu programiert werden soll, hab schon alles mögliche ausprobiert aber der rest des programms funktioniert...
from tkinter import *
#Kommandos
def wechsleBild1():
b1["image"]=img1
lab1 = Label(root,text="ein kleines,\n einsames Kätzchen.\n", fg = "black",
font = "Helvetica 12 bold italic").place(x=205, y=225, width=175, height=70)
return
def wechsleBild2():
b2["image"]=img2
lab2 = Label(root,text="Ihr größter Wunsch\n war es endlich\n einen Freund zum\n Spielen zu finden.\n", fg = "black",
font = "Helvetica 12 bold italic").place(x=395, y=240, width=175, height=85)
return
def wechsleBild3():
b3["image"]=img3
lab3 = Label(root,text="Also begab sie\n sich auf eine Mission!\n", fg = "black",
font = "Helvetica 12 bold italic").place(x=590, y=230, width=175, height=75)
return
def wechsleBild4():
b4["image"]=img4
lab4 = Label(root,text="Auf ihrer Reise\n lagen viele\n Hindernisse,\n gefährliche Anhöhen..\n", fg = "black",
font = "Helvetica 12 bold italic").place(x=795, y=240, width=175, height=85)
return
def wechsleBild5():
b5["image"]=img5
lab5 = Label(root,text="und ewig lange\n Höhlenwege.\n", fg = "black",
font = "Helvetica 12 bold italic").place(x=995, y=235, width=175, height=70)
return
def wechsleBild6():
b6["image"]=img6
lab6 = Label(root,text="Und dann traf sie IHN,\n Onkel Barsik!\n", fg = "black",
font = "Helvetica 12 bold italic").place(x=1190, y=230, width=175, height=85)
return
def wechsleBild7():
b7["image"]=img7
lab7 = Label(root,text="Wie sie diesen alten\n Kater doch hasste!\n Oft hatte sie sich\n schon die schlimmsten\n Szenarien seines\n Todes vorgestellt.\n",
fg = "black", font = "Helvetica 12 bold italic").place(x=205, y=590, width=175, height=120)
return
def wechsleBild8():
b8["image"]=img8
lab8 = Label(root,text="Doch sie war\n vernünftig und\n schlich leise weiter.\n", fg = "black",
font = "Helvetica 12 bold italic").place(x=400, y=585, width=175, height=80)
return
def wechsleBild9():
b9["image"]=img9
lab9 = Label(root,text="Zwischendurch war\n natürlich ein\n Schläfchen und eine\n neue Frisur fällig.\n", fg = "black",
font = "Helvetica 12 bold italic").place(x=600, y=595, width=175, height=85)
return
def wechsleBild10():
b10["image"]=img10
lab10 = Label(root,text="Und was für\n neue Talente sie\n für sich entdeckte!\n", fg = "black",
font = "Helvetica 12 bold italic").place(x=800, y=585, width=175, height=80)
return
def wechsleBild11():
b11["image"]=img11
lab11 = Label(root,text="Dann endlich...\n Auf den ersten Blick\n erkannte sie für sich\n den Freund fürs Leben\n in einem Kücken!\n",
fg = "black", font = "Helvetica 12 bold italic").place(x=1000, y=590, width=175, height=105)
return
def wechsleBild12():
b12["image"]=img12
lab12 = Label(root,text="Heute noch erinnert\n sie sich gerne an\n dieses Abendteuer\n und ihren alten\n Freund das Kücken...\n", fg = "black",
font = "Helvetica 12 bold italic").place(x=1200, y=590, width=175, height=105)
return
def repeat():
command["wechsleBild12"]= b12
command["wechsleBild11"] = b11
command["wechsleBild10"] = b10
command["wechsleBild9"] = b9
command["wechsleBild8"] = b8
command["wechsleBild7"] = b7
command["wechsleBild6"] = b6
command["wechsleBild5"] = b5
command["wechsleBild4"] = b4
command["wechsleBild3"]= b3
command["wechsleBild2"] = b2
command["wechsleBild1"] = b1
return
root = Tk()
#Überschrift
root.title("Meine GUI - Bildergeschichte ")
#Bilder einfügen
img1 = PhotoImage(file="E:\INFO\GUI\Bilder2\Katze1.gif")
img2 = PhotoImage(file="E:\INFO\GUI\Bilder2\Katze2.gif")
img3 = PhotoImage(file="E:\INFO\GUI\Bilder2\Katze3.gif")
img4 = PhotoImage(file="E:\INFO\GUI\Bilder2\Katze4.gif")
img5 = PhotoImage(file="E:\INFO\GUI\Bilder2\Katze5.gif")
img6 = PhotoImage(file="E:\INFO\GUI\Bilder2\Katze6.gif")
img7 = PhotoImage(file="E:\INFO\GUI\Bilder2\Katze7.gif")
img8 = PhotoImage(file="E:\INFO\GUI\Bilder2\Katze8.gif")
img9 = PhotoImage(file="E:\INFO\GUI\Bilder2\Katze9.gif")
img10 = PhotoImage(file="E:\INFO\GUI\Bilder2\Katze10.gif")
img11 = PhotoImage(file="E:\INFO\GUI\Bilder2\Katze11.gif")
img12 = PhotoImage(file="E:\INFO\GUI\Bilder2\Katze12.gif")
#Label oben setzen
lab0 = Label(root, text="Es war einmal...",
fg = "black",
font = "Helvetica 15 bold italic").pack(side=TOP)
#Knöpfe erstellen
b01 = Button(root, text="1", command=wechsleBild1, fg="green")
b02 = Button(root, text="2", command=wechsleBild2, fg="green")
b03 = Button(root, text="3", command=wechsleBild3, fg="green")
b04 = Button(root, text="4", command=wechsleBild4, fg="green")
b05 = Button(root, text="5", command=wechsleBild5, fg="green")
b06 = Button(root, text="6", command=wechsleBild6, fg="green")
b07 = Button(root, text="7", command=wechsleBild7, fg="green")
b08 = Button(root, text="8", command=wechsleBild8, fg="green")
b09 = Button(root, text="9", command=wechsleBild9, fg="green")
b010 = Button(root, text="10", command=wechsleBild10, fg="green")
b011 = Button(root, text="11", command=wechsleBild11, fg="green")
b012 = Button(root, text="12", command=wechsleBild12, fg="green")
b013 = Button(root, text="repeat", command=repeat, fg="green")
b1 = Button(root, text="*Bild*", fg="green")
b2 = Button(root, text="*Bild*", fg="green")
b3 = Button(root, text="*Bild*", fg="green")
b4 = Button(root, text="*Bild*", fg="green")
b5 = Button(root, text="*Bild*", fg="green")
b6 = Button(root, text="*Bild*", fg="green")
b7 = Button(root, text="*Bild*", fg="green")
b8 = Button(root, text="*Bild*", fg="green")
b9 = Button(root, text="*Bild*", fg="green")
b10 = Button(root, text="*Bild*", fg="green")
b11 = Button(root, text="*Bild*", fg="green")
b12 = Button(root, text="*Bild*", fg="green")
#Knöpfe plazieren
b01.place(x=25, y=50, width=50, height=50)
b02.place(x=25, y=100, width=50, height=50)
b03.place(x=25, y=150, width=50, height=50)
b04.place(x=25, y=200, width=50, height=50)
b05.place(x=25, y=250, width=50, height=50)
b06.place(x=25, y=300, width=50, height=50)
b07.place(x=25, y=350, width=50, height=50)
b08.place(x=25, y=400, width=50, height=50)
b09.place(x=25, y=450, width=50, height=50)
b010.place(x=25, y=500, width=50, height=50)
b011.place(x=25, y=550, width=50, height=50)
b012.place(x=25, y=600, width=50, height=50)
b013.place(x=25, y=650, width=50, height=50)
b1.place(x=200, y=50, width=175, height=175)
b2.place(x=400, y=50, width=175, height=175)
b3.place(x=600, y=50,width=175, height=175)
b4.place(x=800, y=50, width=175, height=175)
b5.place(x=1000, y=50, width=175, height=175)
b6.place(x=1200, y=50, width=175, height=175)
b7.place(x=200, y=400, width=175, height=175)
b8.place(x=400, y=400, width=175, height=175)
b9.place(x=600, y=400, width=175, height=175)
b10.place(x=800, y=400, width=175, height=175)
b11.place(x=1000, y=400, width=175, height=175)
b12.place(x=1200, y=400, width=175, height=175)
root.mainloop()