Hallo,
ich will einen button in einer Funktion Verändern: den Text und das state.
Dabei erhalte ich folgende Meldung: start_button["state"]="disabled"
TypeError: 'str' object does not support item assignment
Ich denke der start_button ist doch immer ein "str" Objekt wie soll ich das ändern?
ich habe den Code beigefügt:
import tkinter as tk
from functools import partial
def count_call_back(count_label,time_base, counter_value=1):
count_label["text"] = counter_value
count_label.after(int(time_base*1000), count_call_back, count_label,time_base, counter_value + 1)
def start_action(count_label,eingabe,start_button): # Zeitbasis wird erzeugt, Eingabefehler abgefangen
try:
time_base = float(eingabe.get())
except ValueError:
count_label['text'] = "nur Zahlen!"
else:
count_call_back(count_label,time_base)
start_button["state"]="disabled"
start_button["text"]="inaktiv"
def main():
root = tk.Tk()
root.title(" Zeit-Zähler")
root.minsize(width=400, height=300)
count_label = tk.Label(root, fg="green",bg="white",font=("Arial", 22),width=10,height=2)
count_label.pack(padx=10, pady=10)
text_label=tk.Label(root,text=" Zeitbasis > 0.09 Sek. eingeben",fg="blue")
text_label.pack()
eingabe=tk.Entry(root)
eingabe.pack()
start_button=tk.Button(root,text=" Start ",fg="blue",bg="yellow",width=10,command=partial(start_action,count_label,eingabe,start_button="disable"))
start_button.pack(padx=20,pady=45)
ende_button=tk.Button(root, text="Ende", fg="red", bg="white",width=10,height=1, command=root.destroy)
ende_button.pack(padx=20, pady=20)
root.mainloop()
if __name__ == "__main__":
main()
danke für Hilfe
fehlermeldung bei button disable
- __blackjack__
- User
- Beiträge: 13077
- Registriert: Samstag 2. Juni 2018, 10:21
- Wohnort: 127.0.0.1
- Kontaktdaten:
@reiwe3: Du übergibst da eine Zeichenkette, da müsstest Du aber das `Button`-Objekt übergeben. Da es das zu dem Zeitpunkt noch nicht gibt, kannst Du das `command`-Argument noch nicht beim erstellen den `Button`-Objekts angeben, sondern musst das in einem Extra-Schritt danach machen.
Code: Alles auswählen
#!/usr/bin/env python3
import tkinter as tk
from functools import partial
def count_callback(count_label, time_base, counter_value=1):
count_label["text"] = counter_value
count_label.after(
int(time_base * 1000),
count_callback,
count_label,
time_base,
counter_value + 1,
)
def start_action(count_label, eingabe, start_button):
try:
time_base = float(eingabe.get())
except ValueError:
count_label["text"] = "nur Zahlen!"
else:
count_callback(count_label, time_base)
start_button["state"] = tk.DISABLED
def main():
root = tk.Tk()
root.title("Zeit-Zähler")
root.minsize(width=400, height=300)
count_label = tk.Label(
root, fg="green", bg="white", font=("Arial", 22), width=10, height=2
)
count_label.pack(padx=10, pady=10)
tk.Label(root, text="Zeitbasis > 0.09 Sek. eingeben", fg="blue").pack()
eingabe = tk.Entry(root)
eingabe.pack()
start_button = tk.Button(
root, text="Start", fg="blue", bg="yellow", width=10
)
start_button.pack(padx=20, pady=45)
tk.Button(
root, text="Ende", fg="red", bg="white", width=10, command=root.quit
).pack(padx=20, pady=20)
start_button["command"] = partial(
start_action, count_label, eingabe, start_button
)
root.mainloop()
if __name__ == "__main__":
main()
„All religions are the same: religion is basically guilt, with different holidays.” — Cathy Ladman
danke, danke __blackjack__ ja ,klar der start_button ist als Ganzes ein "str" , ic muss das auftrennen. !
Die Taste kann ich nicht so übergeben! Ich hab das schon einmal gehabt, da war es eine andere Meldung, deshalb bin ich da nicht drauf gekommen. Danke nochmal.
Die Taste kann ich nicht so übergeben! Ich hab das schon einmal gehabt, da war es eine andere Meldung, deshalb bin ich da nicht drauf gekommen. Danke nochmal.