ich beschäftige mich nun seit ein paar Tagen mit Python und möchte eine Anwendung mit folgendem Ziel realisieren:
- Anzeigen eines Nummernfelds mit 0-9
- Anzeigen eines Buttons "öffnen" und "zurück" im selben Raster
- Anhängen gedrückten Zahlen an Variable pin
- Überprüfen und Löschen der Variable pin
Code: Alles auswählen
button = [
['1', '2', '3'],
['4', '5', '6'],
['7', '8', '9'],
['zurück', '0', 'Öffnen'],
]
for y, row in enumerate(button, 1):
for x, key in enumerate(row):
b = tk.Button(width=6,height=3,bg='#BFBFBF',text=button)
b.grid(row=y, column=x, padx=1, pady=1)
Vielen Dank im Voraus für eure Anregungen.
Grüße Patze
Code: Alles auswählen
import tkinter as tk
from PIL import Image, ImageTk
#in dieser Klasse werden Buttons eines Pin Felds erstellt und beim Druck auf
#den Button der entsprechende Wert an die Variable Pin angehängt.
class pin_eingabe(tk.Frame):
def __init__(self, parent):
self.pin = "" # Iniatilisieren von Pin
self.breite = 50 # Button-Breite in px
self.hoehe = 50 # Button-Höhe in px
self.button_farbe = "#BFBFBF" #Button Farbe
self.pixel = tk.PhotoImage(width=1, height=1) #transparentes Image um Größe der Buttons in px zu definieren
self.hintergrund_foto = Image.open("hintergrund.jpg")
self.hintergrund_bild = ImageTk.PhotoImage(self.hintergrund_foto)
self.hintergrund_label = tk.Label(parent, image=self.hintergrund_bild) #Label als Hintergrundbild
self.hintergrund_label.place(x=0, y=0, relwidth=1, relheight=1)
#Definition der Button Eigenschaften
button_1 = tk.Button(width=self.breite,
font = "futura 15 bold",
height=self.hoehe,
bg=self.button_farbe,
compound = "c",
image=self.pixel,
text=1,
command =lambda :self.zahl_eingeben("1"))
button_2 = tk.Button(width=self.breite,
font = "futura 15 bold",
height=self.hoehe,
bg=self.button_farbe,
compound = "c",
image=self.pixel,
text=2,
command =lambda :self.zahl_eingeben("2"))
button_3 = tk.Button(
width=self.breite,
font = "futura 15 bold",
height=self.hoehe,
bg=self.button_farbe,
compound = "c",
image=self.pixel,
text=3,
command =lambda :self.zahl_eingeben("3"))
button_4 = tk.Button(
width=self.breite,
font = "futura 15 bold",
height=self.hoehe,
bg=self.button_farbe,
compound = "c",
image=self.pixel,
text=4,
command =lambda :self.zahl_eingeben("4"))
button_5 = tk.Button(
width=self.breite,
font = "futura 15 bold",
height=self.hoehe,
bg=self.button_farbe,
compound = "c",
image=self.pixel,
text=5,
command =lambda :self.zahl_eingeben("5"))
button_6 = tk.Button(
width=self.breite,
font = "futura 15 bold",
height=self.hoehe,
bg=self.button_farbe,
compound = "c",
image=self.pixel,
text=6,
command =lambda :self.zahl_eingeben("6"))
button_7 = tk.Button(
width=self.breite,
font = "futura 15 bold",
height=self.hoehe,
bg=self.button_farbe,
compound = "c",
image=self.pixel,
text=7,
command =lambda :self.zahl_eingeben("7"))
button_8 = tk.Button(
width=self.breite,
font = "futura 15 bold",
height=self.hoehe,
bg=self.button_farbe,
compound = "c",
image=self.pixel,
text=8,
command =lambda :self.zahl_eingeben("8"))
button_9 = tk.Button(
width=self.breite,
font = "futura 15 bold",
height=self.hoehe,
bg=self.button_farbe,
compound = "c",
image=self.pixel,
text=9,
command =lambda :self.zahl_eingeben("9"))
button_0 = tk.Button(
width=self.breite,
font = "futura 15 bold",
height=self.hoehe,
bg=self.button_farbe,
compound = "c",
image=self.pixel,
text=9,
command =lambda :self.zahl_eingeben("0"))
button_oeffnen = tk.Button(
width=self.breite,
font = "futura 8 bold",
height=self.hoehe,
bg=self.button_farbe,
compound = "c",
image=self.pixel,
text="Öffnen",
command =lambda :self.pin_kontrollieren())
button_loeschen = tk.Button(
width=self.breite,
font = "futura 8 bold",
height=self.hoehe,
bg=self.button_farbe,
compound = "c",
image=self.pixel,
text="Zurück",
command =lambda :self.zahl_loeschen())
#Initalisieren der Buttons
self.abstand_x = 10
self.abstand_y =10
button_1.grid(row=1, column=0, padx=self.abstand_x, pady=self.abstand_y)
button_2.grid(row=1, column=1, padx=self.abstand_x, pady=self.abstand_y)
button_3.grid(row=1, column=2, padx=self.abstand_x, pady=self.abstand_y)
button_4.grid(row=2, column=0, padx=self.abstand_x, pady=self.abstand_y)
button_5.grid(row=2, column=1, padx=self.abstand_x, pady=self.abstand_y)
button_6.grid(row=2, column=2, padx=self.abstand_x, pady=self.abstand_y)
button_7.grid(row=3, column=0, padx=self.abstand_x, pady=self.abstand_y)
button_8.grid(row=3, column=1, padx=self.abstand_x, pady=self.abstand_y)
button_9.grid(row=3, column=2, padx=self.abstand_x, pady=self.abstand_y)
button_0.grid(row=4, column=1, padx=self.abstand_x, pady=self.abstand_y)
button_loeschen.grid(row=4, column=0, padx=self.abstand_x, pady=self.abstand_y)
button_oeffnen.grid(row=4, column=2, padx=self.abstand_x, pady=self.abstand_y)
self.pin_anzeige = tk.Entry(width=20, font = "futura 15 bold" )
self.pin_anzeige.grid(row=0, column=0, columnspan=3, ipady=7, pady=30, padx=20)
#Anhägen des Buttonwert an "pin"
def zahl_eingeben(self,zahl):
self.pin_anzeige["bg"] = "white"
self.pin_anzeige.delete('0', 'end')
print(self.pin)
self.pin += zahl
print(self.pin)
self.pin_anzeige.insert('end',self.pin)
#Löschen der letzten Zahl
def zahl_loeschen(self):
self.pin_anzeige["bg"] = "white"
self.pin_anzeige.delete('0', 'end')
print(self.pin)
self.pin = self.pin[:-1]
print(self.pin)
self.pin_anzeige.insert('end',self.pin)
#Überprüfen von Pin
def pin_kontrollieren(self):
if self.pin == "1988":
self.pin_anzeige["bg"] = "green"
else:
print('Pin wiederholen')
self.pin_anzeige["bg"]="red"
self.pin = '' # Pin leeren
self.pin_anzeige.delete('0', 'end')
def main():
root = tk.Tk()
w, h = 800, 460
root.geometry("%dx%d+0+0" % (w, h))
root.configure(background='white')
fenster = pin_eingabe(root)
root.mainloop()
if __name__ == '__main__':
main()