Python Forum

Full Version: Dice roller with Tkinter GUI
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
GUI
from tkinter import *
import dice_roller

root = Tk()


class App:

	def __init__(self, master):

		self.title = Label(master, fg="black", text="Dice Roller")
		self.output = Label(master, fg="black", bg="white", text="Output", height=20, width=40) #TODO: add text function
		self.nb_dices_entry = Entry(master)
		self.nb_faces_entry = Entry(master)
		self.nb_dices_label = Label(master, fg="black", text="How many dices?:")
		self.nb_faces_label = Label(master, fg="black", text="How many faces on each dices?:")
		self.generate_button = Button(master, text="Generate Dices", command=self.get_output)
		self.quit_button = Button(master, text="QUIT", command=master.quit)

		self.log = IntVar()
		self.log_results = Checkbutton(master, text="Log in dice_result.txt", variable=self.log) #LOG = TRUE or FALSE


		self.title.grid(row=0, columnspan=2)
		self.output.grid(row=1, columnspan=2)
		self.nb_dices_entry.grid(row=2, column=1)
		self.nb_dices_label.grid(row=2, sticky=E)
		self.nb_faces_entry.grid(row=3, column=1)
		self.nb_faces_label.grid(row=3, sticky=E)
		self.log_results.grid(row=4, columnspan=2)
		self.generate_button.grid(row=5)
		self.quit_button.grid(row=5, column=1)

	def get_output(self):
		nb_dice = int(self.nb_dices_entry.get())
		nb_face = int(self.nb_faces_entry.get())
		log = self.log.get()
		output = dice.dice_roller(nb_dice, nb_face, log)
		self.output["text"] = output


app = App(root)

root.mainloop()
and dice_roller
from random import randint

def dice_roller(nb_dice, nb_face, log):
	if log:
		file = open("dice_result.txt", "w")
		file.truncate()
	dice_rolled = 0
	total = 0
	result_list = setup_list(nb_face)
	while dice_rolled < nb_dice:
		result = randint(1, nb_face)
		dice_rolled += 1
		result_list[result-1] += 1
		total += result
		if log:
			write_result(result, dice_rolled, file)
	total = create_output(result_list, total)
	if log:
		write_total(total, file)
	return total

def write_total(total, file):
	file.write(total)

def write_result(result, roll_id, file):
	string = f"Dice #{roll_id}: {result}\n"
	file.write(string)

def create_output(result_list, total):
	value_print = ""
	values_done = 0
	for result in result_list:
		values_done += 1
		value_print = value_print + str(result) + " dice with value " + str(values_done) + "\n"
	total = value_print + "\nDice total: " + str(total)
	return total

def setup_list(nb_results):
	values_made = 0
	my_list = []
	while values_made < nb_results:
		my_list.append(0)
		values_made += 1
	return my_list
Any tips/comments are appreciated!
Looking at the dice roller file:
  • The while loop should in dice_roller should be a for loop (for roll in range(nb_dice):)
  • write_result is inefficient. You are recreating that string every time through the loop. Create the string once before the loop, and write the line without a function.
  • I would remove write_total and just put that one line of code in the loop in dice_roller. It's just adding a function call to every iteration of the loop.
  • setup_list can be replaced with [0] * nb_results. I would actually make it one longer. Then you can just add one to result_list[result] on line 13. That saves another subtraction each loop iteration.
  • Your variable and function names are nice and clear. The next step is to start commenting your code, and adding docstrings to your functions.