Seite 1 von 1

frame bildbutton

Verfasst: Sonntag 9. Juni 2013, 11:14
von tornado1
kann mir hier vielleicht jmd helfen ich habe diesen tkinter erstellt aber beim 2. fenster werden keine bilder angezeigt geschweige denn buttons (die def zeilen sind eingerückt) :

Code: Alles auswählen

from Tkinter import*

Fenster=Tk()

Frame = Frame(Fenster, width=100, height=100)
Frame.pack()

def login():
    Frame.destroy()
    Fenster.geometry('280x280')
    Fenster.configure(bg='darkgreen')

    Image=PhotoImage(file="minifussball.gif")

    Image2=PhotoImage(file="minibasketball.gif")

    Image3=PhotoImage(file="minivolleyball.gif")

    Image4=PhotoImage(file="minihandball.gif")

    b1=Button(Fenster, image=Image, width=100, height=100)
    b1.place(x=20,y=20)

    b2=Button(Fenster, image=Image2, width=100, height=100)
    b2.place(x=150,y=20)

    b3=Button(Fenster, image=Image3, width=100, height=100)
    b3.place(x=150,y=150)

    b4=Button(Fenster, image=Image4, width=100, height=100)
    b4.place(x=20,y=150)

Image=PhotoImage(file="tutorial-img-big-2159.gif")

background = Label(Frame, image=Image)
background.pack()

btn=Button(Frame,text='login', command=login, height=2, width=16)
btn.place(x=132,y=100)

e1 = Entry(Frame)
e2 = Entry(Frame)

e1.grid()
e2.grid()

e1.place(x=130,y=70)
e2.place(x=130,y=50)

text1=Label(Frame,text='Benutzername', width=13)
text1.place(x=20,y=50)

text2=Label(Frame,text='Passwort', width=13)
text2.place(x=20,y=70)

text3=Label(Frame,text='Sportergebnisse', height=2, width=35)
text3.place(x=20,y=10) 

Fenster.mainloop()

Re: frame bildbutton

Verfasst: Sonntag 9. Juni 2013, 11:37
von BlackJack
@tornado1: Die Bildobjekte müssen auf Python-Seite erreichbar bleiben damit Tk sie anzeigen kann. Die Python-Laufzeitumgebung hat keine Ahnung ob und wie lange die Bilddaten von Tk verwendet werden, darum können die Bildobjekte aus Pythonsicht nach Ablauf der `login()`-Funktion entsorgt werden.

Eine saubere und vernünftige Lösung wäre objektorientiert. Objektorientierung ist IMHO sowieso Vorraussetzung wenn man GUIs mit `Tkinter` programmieren will.

Weitere Anmerkungen: Den Sternchenimport sollte man sein lassen, damit müllt man sich den Modulnamensraum mit ca. 200 Namen voll. Üblich ist ``import Tkinter as tk``.

Auf Modulebene sollte kein Code stehen der nicht nur dazu dient Konstanten, Klassen, und Funktionen zu definieren. Das macht den Quelltext übersichtlicher und weniger fehleranfällig, weil man dann nicht in Versuchung kommt etwas zu verwenden was eine Funktion oder Methode nicht als Argument betreten hat.

Namensgebung und Leerzeichensetzung halten sich nicht an den Style Guide for Python Code.

`geometry()` und insbesondere `place()` sollte man vermeiden, da das zu GUIs führt die nur auf dem Rechner wo sie programmiert wurden, so aussehen wie man das erwartet. Die Variationen von Auflösungen, Bildschirmgrössen, und Darstellunsgeinstellungen sind heutzutage so gross, dass eine GUI die mit `place()` auf einem Rechner gut aussieht auf einem anderen unbenutzbar sein kann. Ausserdem ist das Ändern des Layouts unnötig umständlich wenn man mehr als ein oder zwei GUI-Elemente verschieben möchte.

Re: frame bildbutton

Verfasst: Sonntag 9. Juni 2013, 15:37
von tornado1
also wie müsste ich das programnm schreiben damit die bilder angezeigt werden?

Re: frame bildbutton

Verfasst: Montag 10. Juni 2013, 09:30
von BlackJack
@tornado1: Komplett anders. Objektorientiert.

Re: frame bildbutton

Verfasst: Dienstag 11. Juni 2013, 17:31
von vegaseat
Beispiel einer Bildrettung ...

Code: Alles auswählen

# create a button with an image and optional text on it

try:
    # Python2
    import Tkinter as tk
except ImportError:
    # Python3
    import tkinter as tk

def action():
    """do something with the button"""
    root.title("button pressed")

root = tk.Tk()
w = 200
h = 100
x = 150
y = 100
# use width x height + x_offset + y_offset (no spaces!)
root.geometry("%dx%d+%d+%d" % (w, h, x, y))
#frame = tk.Frame(root)

# tk reads only GIF and PGM/PPM images
photo = tk.PhotoImage(file="Fun_small.gif")
'''
# for other image files use ...
from PIL import Image, ImageTk
image = Image.open("Fun_small.jpg")
photo = ImageTk.PhotoImage(image)
'''
# create the image button
# compound='top' implies image is located above the optional text
image_button = tk.Button(root, compound='top', width=55, 
    height=65, image=photo, text="click me!", command=action)
# save button image from garbage collection!  <---- Rettung
image_button.image = photo
image_button.pack(side='left', padx=2, pady=2)

# run the event loop
root.mainloop()