Seite 1 von 1

Funktion erst nach Klick auf Button aktivieren

Verfasst: Montag 7. Mai 2018, 16:45
von Blue-Cid
Hi,

ich würde gerne durch den Klick auf einen Button eine Aktivierung für einen weiteren Button und eine Funktion erreichen.

Solange der Button für die Aktivierung nicht gedrückt wurde, soll der zu aktivierende Button und die zu aktivierende Funktion deaktiviert sein.

Hier ist mein Code (Danke nochmal an wuf für die Hilfe!)

Code: Alles auswählen

 import tkinter
 
def handleButton(event):
   top.i = top.i + 1
   top.v['text'] = "Aufzählung: "+str(top.i)
   pushButton()
         
 
def pushButton(event=None):
   top.zero = top.zero + 1
   total = top.i / top.zero * 10
   print(top.i, top.zero, total)
   top.f['text'] = "" + str(total)
 
top = tkinter.Tk()
 
top.i = 0
top.zero = 0
 
# AUSGABE AUFZÄHLUNG
 
top.v = tkinter.Label(top, bg='gray95', text='Aufzählung: 0', relief='sunken')
top.v.grid(row=0, column=2, sticky='wesn', pady=2)
 
# AUSGABE WERT2
 
top.f = tkinter.Label(top, bg='gray95', text='Wert 2: ', relief='sunken')
top.f.grid(row=1, column=2, sticky='wesn', pady=2)
 
 
#BUTTONS
 
b = tkinter.Button(top, text='Handle Button')
b.grid(row=0, column=1, sticky='wesn')
b.bind('<Button-1>', handleButton)
 
b0 = tkinter.Button(top, text='Push Button')
b0.grid(row=1, column=1,  sticky='wesn')
b0.bind('<Button-1>', pushButton)

ba = tkinter.Button(top, text='Aktivierung')
ba.grid(row=1, column=0,  sticky='wesn')
 
 
top.mainloop()
Der Button ba soll hierbei den Button b0 aktivieren + die dazugehörige Funktion pushButton.

Danke schon mal fürs Lesen!

LG

Re: Funktion erst nach Klick auf Button aktivieren

Verfasst: Montag 7. Mai 2018, 18:45
von Blue-Cid
Ich habe es jetzt geschafft den Button deaktiviert zu starten. Außerdem habe ich es geschafft den Button mit dem Aktivierungsbutton zu aktivieren.

Das Problem ist aber, wenn ich den deaktivierten Button drücke, dann wird die Funktion trotzdem ausgeführt.

Hier nochmal der Code:

Code: Alles auswählen

import tkinter
 
def handleButton(event):
   top.i = top.i + 1
   top.v['text'] = "Aufzählung: "+str(top.i)
   pushButton()
         
 
def pushButton(event=None):
   top.zero = top.zero + 1
   total = top.i / top.zero * 10
   print(top.i, top.zero, total)
   top.f['text'] = "" + str(total)

def Aktivierung(event):
	b0.config(state="normal")
 
top = tkinter.Tk()
 
top.i = 0
top.zero = 0
 
# AUSGABE AUFZÄHLUNG
 
top.v = tkinter.Label(top, bg='gray95', text='Aufzählung: 0', relief='sunken')
top.v.grid(row=0, column=2, sticky='wesn', pady=2)
 
# AUSGABE WERT2
 
top.f = tkinter.Label(top, bg='gray95', text='Wert 2: ', relief='sunken')
top.f.grid(row=1, column=2, sticky='wesn', pady=2)
 
 
#BUTTONS
 
b = tkinter.Button(top, text='Handle Button')
b.grid(row=0, column=1, sticky='wesn')
b.bind('<Button-1>', handleButton)
 
b0 = tkinter.Button(top, text='Push Button')
b0.grid(row=1, column=1,  sticky='wesn')
b0.bind('<Button-1>', pushButton)
b0.config(state="disabled")

ba = tkinter.Button(top, text='Aktivierung')
ba.grid(row=1, column=0,  sticky='wesn')
ba.bind('<Button-1>', Aktivierung)
 
 
top.mainloop()

Re: Funktion erst nach Klick auf Button aktivieren

Verfasst: Montag 7. Mai 2018, 19:25
von Blue-Cid
OK ich habs hinbekommen.

Jetzt würde ich gerne, dass der Aktivierungsbutton beim erneuten Klicken wieder deaktiviert wird.

Hier wieder der neue Code:

Code: Alles auswählen

import tkinter
 
def handleButton(event):
   top.i = top.i + 1
   top.v['text'] = "Aufzählung: "+str(top.i)
   
         
 
def pushButton(event=None):
	state = str(b0['state'])
	if state == 'normal':
	   top.zero = top.zero + 1
	   total = top.i / top.zero * 10
	   print(top.i, top.zero, total)
	   top.f['text'] = "" + str(total)

def Aktivierung(event):
	b0.config(state="normal")
 
top = tkinter.Tk()
 
top.i = 0
top.zero = 0
 
# AUSGABE AUFZÄHLUNG
 
top.v = tkinter.Label(top, bg='gray95', text='Aufzählung: 0', relief='sunken')
top.v.grid(row=0, column=2, sticky='wesn', pady=2)
 
# AUSGABE WERT2
 
top.f = tkinter.Label(top, bg='gray95', text='Wert 2: ', relief='sunken')
top.f.grid(row=1, column=2, sticky='wesn', pady=2)
 
 
#BUTTONS
 
b = tkinter.Button(top, text='Handle Button')
b.grid(row=0, column=1, sticky='wesn')
b.bind('<Button-1>', handleButton)
 
b0 = tkinter.Button(top, text='Push Button')
b0.grid(row=1, column=1,  sticky='wesn')
b0.bind('<Button-1>', pushButton)
b0.config(state="disabled")

ba = tkinter.Button(top, text='Aktivierung')
ba.grid(row=1, column=0,  sticky='wesn')
ba.bind('<Button-1>', Aktivierung)
 
 
top.mainloop()

Re: Funktion erst nach Klick auf Button aktivieren

Verfasst: Montag 7. Mai 2018, 21:03
von Sirius3
@Blue-Cid: noch ein paar Anmerkungen. Eingerückt wird immer 4 Leerzeichen pro Ebene. Variablennamen sollten sprechend sein, einbuchstabige sind das sicher nicht. Statt Strings mit + und str zusammenzustückeln benutzt man .format. Die state-Prüfung ist unnötig, weil die Funktion ja bei disabeltem Knopf nicht aufgerufen werden kann.

Mal benutzt Du den Index-Zugriff, mal config um Optionen zu setzen. Entscheide Dich für eins.
Statt bind benutzt man command bei Knöpfen.

Wenn Du schon Werte an Tk-Objekte binden willst, dann benutze kein globales Masterobjekt, sondern lokale Labels.

Code: Alles auswählen

from functools import partial
import tkinter
 
def handle(aufzaehlung):
    aufzaehlung.value += 1
    aufzaehlung['text'] = "Aufzählung: {}".format(aufzaehlung.value)

def push(aufzaehlung, wert):
    wert.value += 1
    total = aufzaehlung.value / wert.value * 10
    wert['text'] = "{:.4}".format(total)

def aktivierung(button):
    button["state"] = "normal"

def main():
    top = tkinter.Tk()
 
    aufzaehlung = tkinter.Label(top, bg='gray95', text='Aufzählung: 0', relief='sunken')
    aufzaehlung.grid(row=0, column=2, pady=2)
    aufzaehlung.value = 0
 
    wert = tkinter.Label(top, bg='gray95', text='Wert 2: ', relief='sunken')
    wert.grid(row=1, column=2, pady=2, sticky='wesn')
    wert.value = 0
 
    b = tkinter.Button(top, text='Handle Button',
        command=partial(handle, aufzaehlung))
    b.grid(row=0, column=1, sticky='wesn')
 
    push_button = tkinter.Button(top, text='Push Button',
        command=partial(push, aufzaehlung, wert),
        state="disabled")
    push_button.grid(row=1, column=1, sticky='wesn')

    b = tkinter.Button(top, text='Aktivierung',
        command=partial(aktivierung, push_button))
    b.grid(row=1, column=0, sticky='wesn')
    
    top.mainloop()

if __name__ == '__main__':
    main()

Re: Funktion erst nach Klick auf Button aktivieren

Verfasst: Dienstag 8. Mai 2018, 20:56
von wuf
Hi Blue Cid

Kannst du das folgende geänderte Skript einmal ausprobieren:

Code: Alles auswählen

import tkinter
 
def handleButton(event):
   top.i = top.i + 1
   top.v['text'] = "Aufzählung: "+str(top.i)
   
def pushButton(event=None):
   #state = str(b0['state'])
   #if state == 'normal':
       
  top.zero = top.zero + 1
  total = top.i / top.zero * 10
  print(top.i, top.zero, total)
  top.f['text'] = "" + str(total)

def Aktivierung(event):
    if b0['state'] == 'disabled':
        b0.config(state="normal")
    else:
        b0.config(state="disable")
        
top = tkinter.Tk()
 
top.i = 0
top.zero = 0
 
# AUSGABE AUFZÄHLUNG
 
top.v = tkinter.Label(top, bg='gray95', text='Aufzählung: 0', relief='sunken')
top.v.grid(row=0, column=2, sticky='wesn', pady=2)
 
# AUSGABE WERT2
 
top.f = tkinter.Label(top, bg='gray95', text='Wert 2: ', relief='sunken')
top.f.grid(row=1, column=2, sticky='wesn', pady=2)
 
 
#BUTTONS
 
b = tkinter.Button(top, text='Handle Button')
b.grid(row=0, column=1, sticky='wesn')
b.bind('<Button-1>', handleButton)
 

#b0 = tkinter.Button(top, text='Push Button')
#b0.grid(row=1, column=1,  sticky='wesn')
#b0.bind('<Button-1>', pushButton)
#b0.config(state="disabled")

b0 = tkinter.Button(top, text='Push Button', state='disabled',
    command= pushButton)
b0.grid(row=1, column=1,  sticky='wesn')

ba = tkinter.Button(top, text='Aktivierung')
ba.grid(row=1, column=0,  sticky='wesn')
ba.bind('<Button-1>', Aktivierung)
 
 
top.mainloop()
Gruss wuf :wink: