cookchar

Dice Roll DnD Stat Chooser

Jan 11th, 2018
94
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import tkinter as tk
  2. import tkinter.simpledialog as sdg
  3. import tkinter.messagebox as mbx
  4. import tkinter.font as tkf
  5. import random as rnd
  6.  
  7. class diceRollStatsRoot(sdg.Dialog):
  8.     def body(self, master):
  9.         self.title("New Character: Dice Roll Stats")
  10.         self.resizable(False, False)
  11.         self.letters = ('A', 'B', 'C', 'D', 'E', 'F')
  12.         self.rolled = False
  13.         self.rolls = [0, 0, 0, 0, 0, 0]
  14.         self.rollsVar = tk.StringVar()
  15.         self.rollsVar.set("Press \"Roll\" to begin!")
  16.  
  17.         tk.Label(master, text = "Set each stat to a unique lettered value", font = ftSmall).grid(row = 0, columnspan = 3)
  18.  
  19.         rollsLbl = tk.Label(master, textvariable = self.rollsVar, width = 36, relief = "ridge", font = ftNormal)
  20.         rollsLbl.grid(row = 1, column = 0, columnspan = 2)
  21.  
  22.         tk.Button(master, text = "Roll", command = self.rollStats, cursor = "hand2", font = ftNormal).grid(row = 1, column = 2)
  23.  
  24.         tk.Label(master, text = "Strength", font = ftNormal).grid(row = 2, column = 0)
  25.         tk.Label(master, text = "Dexterity", font = ftNormal).grid(row = 3, column = 0)
  26.         tk.Label(master, text = "Constitution", font = ftNormal).grid(row = 4, column = 0)
  27.         tk.Label(master, text = "Intelligence", font = ftNormal).grid(row = 5, column = 0)
  28.         tk.Label(master, text = "Wisdom", font = ftNormal).grid(row = 6, column = 0)
  29.         tk.Label(master, text = "Charisma", font = ftNormal).grid(row = 7, column = 0)
  30.  
  31.         self.STRSbx = tk.Spinbox(master, values = self.letters)
  32.         self.DEXSbx = tk.Spinbox(master, values = self.letters)
  33.         self.CONSbx = tk.Spinbox(master, values = self.letters)
  34.         self.INTSbx = tk.Spinbox(master, values = self.letters)
  35.         self.WISSbx = tk.Spinbox(master, values = self.letters)
  36.         self.CHASbx = tk.Spinbox(master, values = self.letters)
  37.  
  38.         self.STRSbx.grid(row = 2, column = 1, columnspan = 2)
  39.         self.DEXSbx.grid(row = 3, column = 1, columnspan = 2)
  40.         self.CONSbx.grid(row = 4, column = 1, columnspan = 2)
  41.         self.INTSbx.grid(row = 5, column = 1, columnspan = 2)
  42.         self.WISSbx.grid(row = 6, column = 1, columnspan = 2)
  43.         self.CHASbx.grid(row = 7, column = 1, columnspan = 2)
  44.  
  45.  
  46.     def validate(self):
  47.         if not self.rolled:
  48.             mbx.showerror("Error", "You must roll at least once!")
  49.             return 0
  50.  
  51.         selections = []
  52.         selections.append(self.STRSbx.get())
  53.         selections.append(self.DEXSbx.get())
  54.         selections.append(self.CONSbx.get())
  55.         selections.append(self.INTSbx.get())
  56.         selections.append(self.WISSbx.get())
  57.         selections.append(self.CHASbx.get())
  58.  
  59.         for l in self.letters:
  60.             if selections.count(l) > 1:
  61.                 mbx.showerror("Error", "Each lettered value must be used, and used only once!")
  62.                 return 0
  63.  
  64.         return 1
  65.  
  66.     def apply(self):
  67.         resultString = ""
  68.  
  69.         resultString += str(self.rolls[self.letters.index(self.STRSbx.get())]) + "|"
  70.         resultString += str(self.rolls[self.letters.index(self.DEXSbx.get())]) + "|"
  71.         resultString += str(self.rolls[self.letters.index(self.CONSbx.get())]) + "|"
  72.         resultString += str(self.rolls[self.letters.index(self.INTSbx.get())]) + "|"
  73.         resultString += str(self.rolls[self.letters.index(self.WISSbx.get())]) + "|"
  74.         resultString += str(self.rolls[self.letters.index(self.CHASbx.get())]) + "\n"
  75.         resultString += str((self.rolls[self.letters.index(self.STRSbx.get())] - 10) // 2) + "|"
  76.         resultString += str((self.rolls[self.letters.index(self.DEXSbx.get())] - 10) // 2) + "|"
  77.         resultString += str((self.rolls[self.letters.index(self.CONSbx.get())] - 10) // 2) + "|"
  78.         resultString += str((self.rolls[self.letters.index(self.INTSbx.get())] - 10) // 2) + "|"
  79.         resultString += str((self.rolls[self.letters.index(self.WISSbx.get())] - 10) // 2) + "|"
  80.         resultString += str((self.rolls[self.letters.index(self.CHASbx.get())] - 10) // 2)
  81.  
  82.         self.result = resultString
  83.     def rollStats(self):
  84.         tempStats = [0, 0, 0, 0, 0, 0]
  85.         tempRolls = []
  86.         tempStr = ""
  87.  
  88.         for i in range(len(tempStats)):
  89.             for j in range(4):
  90.                 tempRolls.append(rnd.randint(1,6))
  91.  
  92.             tempRolls.remove(min(tempRolls))
  93.  
  94.             tempStats[i] = sum(tempRolls)
  95.  
  96.             tempRolls = []
  97.  
  98.         for i in range(len(tempStats)):
  99.             self.rolls[i] = tempStats[i]
  100.             tempStr += self.letters[i] + ":" + str(tempStats[i])
  101.             if i < 5:
  102.                 tempStr += ", "
  103.  
  104.         self.rollsVar.set(tempStr)
  105.         self.rolled = True
  106.  
  107. base = tk.Tk()
  108.  
  109. ftSmall = tkf.Font(family = "Consolas", size = 8)
  110. ftNormal = tkf.Font(family = "Consolas", size = 10)
  111.  
  112. diceRollStatsRoot(base)
RAW Paste Data