I'm thinking of making a little strategic game using tkinter (player should interact with it using buttons). But my code is very repetitive. This is a mock-up of my program:
import tkinter as tk
import random
def multi(*args):
for func in args:
func()
def show_hide():
if not button["text"]:
button.configure(text="Perform action #1", bd=2, bg="white", command=activate_deactivate)
status_label["text"] = "Status: {}".format(status.get())
button_2.configure(text="Perform action #2", bd=2, bg="white", command=activate_deactivate_2)
status_label_2["text"] = "Status: {}".format(status_2.get())
else:
button.configure(text="", bd=0, bg="#F0F0F0", command=None)
status_label["text"] = ""
button_2.configure(text="", bd=0, bg="#F0F0F0", command=None)
status_label_2["text"] = ""
def activate_deactivate():
if status.get() == "Can be done":
status.set("To be done")
status_label.configure(text="Status: {}".format(status.get()), fg="blue")
else:
status.set("Can be done")
status_label.configure(text="Status: {}".format(status.get()), fg="black")
def activate_deactivate_2():
if status_2.get() == "Can be done":
status_2.set("To be done")
status_label_2.configure(text="Status: {}".format(status.get()), fg="blue")
else:
status_2.set("Can be done")
status_label_2.configure(text="Status: {}".format(status.get()), fg="black")
def step_forward():
if status.get() == "To be done":
button.configure(text="", bd=0, bg="#F0F0F0", state="disabled")
status_label["text"] = ""
result = random.choice(["success", "failure"])
if result == "success":
status.set("Accomplished")
status_label["fg"] = "green"
else:
status.set("Failed")
status_label["fg"] = "red"
else:
button.configure(text="", bd=0, bg="#F0F0F0", command=None)
status_label["text"] = ""
def step_forward_2():
if status_2.get() == "To be done":
button_2.configure(text="", bd=0, bg="#F0F0F0", state="disabled")
status_label_2["text"] = ""
result = random.choice(["success", "failure"])
if result == "success":
status_2.set("Accomplished")
status_label_2["fg"] = "green"
else:
status_2.set("Failed")
status_label_2["fg"] = "red"
else:
button_2.configure(text="", bd=0, bg="#F0F0F0", command=None)
status_label_2["text"] = ""
root = tk.Tk()
status = tk.StringVar()
status.set("Can be done")
status_2 = tk.StringVar()
status_2.set("Can be done")
main = tk.Button(root, text="Show/Hide", bg="white", font="courier 30", command=show_hide)
main.pack()
frame = tk.Frame(root, pady=10)
frame.pack()
frame_1 = tk.Frame(frame, padx=10)
frame_1.pack(side="left")
frame_2 = tk.Frame(frame, padx=10)
frame_2.pack(side="left")
button = tk.Button(frame_1, text="", font="courier 20", bd=0)
button.grid(row=0, column=0)
status_label = tk.Label(frame_1, text="", font="courier 14")
status_label.grid(row=1, column=0)
button_2 = tk.Button(frame_2, text="", font="courier 20", bd=0)
button_2.grid(row=0, column=1)
status_label_2 = tk.Label(frame_2, text="", font="courier 14")
status_label_2.grid(row=1, column=1)
next_day = tk.Button(root, text="Next day", bg="white", font="courier 30",
command=lambda: multi(step_forward, step_forward_2))
next_day.pack()
root.mainloop()
This definitely looks terrible. With blank lines, it takes 100+ lines of code just to make 2 buttons (well, it's not only "2 buttons"), but I want to have many of these (a few groups each one containing around 4+ buttons which is a lot). If you know how "this" can be changed, I'd love to hear any suggestions (although this code is bad, it perfectly represents functionality and look of the interactive part of the UI, so these should remain the same).