Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import tkinter as tk
- import copy as c
- # u/PotentBeverage 22/09/2020
- # data classes
- class Character:
- """Basic character dataclass"""
- def __init__(self, name, initiative):
- self.name = name
- self.initiative = initiative
- def __str__(self):
- return f"{self.name} ({self.initiative})"
- # important variables
- orders: [Character] = [Character("End of Round", -1)]
- # functions
- def add_character(name: str, initiative: int):
- """Adds a character to the order list"""
- global orders
- orders.append(Character(name, initiative))
- def remove_character(index: int):
- """Removes a character from the orders list via index"""
- global orders
- try:
- del orders[index]
- except IndexError as e:
- print(e)
- def sort_orders():
- """Sorts the orders list based on initiative"""
- global orders
- orders.sort(key=lambda cha: cha.initiative, reverse=True)
- def move_character():
- """Move the character in 0th place to last place"""
- global orders
- orders.append(orders.pop(0))
- def pretty_print(l_in):
- """Pretty Print a input list"""
- out = ""
- for i in [str(i) for i in l_in]:
- out += i + "\n"
- return out
- # main tk classes
- class Main(tk.Frame):
- def __init__(self, base):
- super().__init__(base)
- self.base = base
- self.base.title("Initiatives")
- self.base.geometry("480x480")
- self.grid()
- # creating widgets
- # title
- self.title = tk.Label(self.base, text="Initiatives", font=("Arial", 24))
- self.title.grid(column=0, columnspan=2, row=0, sticky="ew") # east + west
- self.createform()
- self.__c = tk.Label(self.base) # double underscore labels serve as padding with tk.grid
- self.__c.grid(column=0, row=5)
- self.orders = tk.Text(self.base, width=30, height=20)
- self.orders.grid(column=0, columnspan=2, row=10, sticky="ns")
- self.orders.config(state=tk.DISABLED)
- self.__b = tk.Label(self.base, text=" ")
- self.__b.grid(column=2, row=0)
- self.createcancel()
- self.generate = tk.Button(self.base, text="GENERATE", font=("Arial", 14), command=self.generatelist)
- self.generate.grid(column=3, row=10, sticky="n")
- self.base.bind("<Return>", self.on_enter)
- def createform(self):
- """Creates the entry form for adding a character to the orders list"""
- self.l_name = tk.Label(self.base, text="Name")
- self.l_name.grid(column=0, row=1, sticky="ew")
- self.l_init = tk.Label(self.base, text="Initiative")
- self.l_init.grid(column=1, row=1, sticky="ew")
- self.f_name = tk.Entry(self.base)
- self.f_name.grid(column=0, row=2)
- self.f_init = tk.Entry(self.base)
- self.f_init.grid(column=1, row=2)
- self.__a = tk.Label(self.base)
- self.__a.grid(column=0, row=3)
- self.b_addchar = tk.Button(self.base, text="Add Character", command=self.addtoform)
- self.b_addchar.grid(column=0, row=4)
- self.b_clear = tk.Button(self.base, text="Clear Entries", command=self.clearform)
- self.b_clear.grid(column=1, row=4)
- def createcancel(self):
- """Create a form which removes a character based on index"""
- self.l_remv = tk.Label(self.base, text="(Remove) Index")
- self.l_remv.grid(column=3, row=1)
- self.f_remv = tk.Entry(self.base)
- self.f_remv.grid(column=3, row=2, sticky="ew")
- self.b_remv = tk.Button(self.base, text="Remove Character?", command=self.removefromform)
- self.b_remv.grid(column=3, row=4)
- def clearform(self):
- """Clears all data in all entry boxes and prints orders to the text component"""
- self.f_init.delete(0, tk.END)
- self.f_name.delete(0, tk.END)
- self.f_remv.delete(0, tk.END)
- self.displayintext(pretty_print(orders))
- def addtoform(self):
- """button command to add a character, interfaces with add_character()"""
- try:
- fi = int(self.f_init.get())
- except ValueError as e:
- print(e)
- return
- add_character(self.f_name.get(), fi)
- # print("orders", [str(i) for i in orders])
- self.clearform()
- def removefromform(self):
- """Button command to remove a character, interfaces with remove_character()"""
- try:
- ind = int(self.f_remv.get())
- except ValueError as e:
- print(e)
- return
- remove_character(ind)
- self.clearform()
- def displayintext(self, text):
- """Inserts provided text into the text box widget"""
- self.orders.configure(state='normal')
- self.orders.delete("1.0", tk.END)
- self.orders.insert(tk.END, str(text))
- self.orders.configure(state='disabled')
- def on_enter(self, event):
- """RETURN key shortcut to add to form"""
- self.addtoform()
- def generatelist(self):
- """Generates a separate window which tracks initiatives"""
- sort_orders() # sorts orders
- self.generatedlist = GeneratedInitiativeTracker(self.base, orders)
- class GeneratedInitiativeTracker(tk.Toplevel):
- def __init__(self, base, orders_instance):
- super().__init__(base)
- self._roundnumber = 0
- # some labels to help along
- self.t = tk.Label(self, text="Turn List", font=("Arial", 20))
- self.t.grid(column=1, row=0, sticky="ew") # east + west
- self.rn = tk.Label(self, text=f"Round No: {self._roundnumber}", justify=tk.CENTER)
- self.rn.grid(column=1, row=1)
- self.orders = c.deepcopy(orders_instance)
- self.title("Initiative Tracker:")
- self.buttons: [tk.Button] = []
- self.createbuttons()
- self.bind("<Return>", self.on_enter)
- def createbuttons(self):
- """
- Every character in orders is assigned their own button, which when pressed will move them to the
- bottom of the queue. The buttons are displayed as a list going down column 0
- """
- for char in orders:
- nb = tk.Button(self, text=str(char), command=self.movenext)
- self.buttons.append(nb)
- for i in range(len(self.buttons)):
- self.buttons[i].grid(row=i, column=0, sticky="ew")
- def movenext(self):
- """Button interface to move them along, and increment round counterb by 1 if End of Round is pressed"""
- # detect if button is end of round
- if str(orders[0]) == "End of Round (-1)":
- self._roundnumber += 1
- self.rn["text"] = f"Round No: {self._roundnumber}"
- # move the buttons
- move_character()
- while self.buttons:
- self.buttons.pop().destroy()
- self.createbuttons()
- def on_enter(self, event):
- """Enter key shortcut also moves"""
- self.movenext()
- # main tk loop
- root = tk.Tk()
- mwin = Main(root)
- mwin.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement