Python Forum
Dice roller with Tkinter GUI
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Dice roller with Tkinter GUI
#1
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!
Reply
#2
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.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  A CLI based Dice Roller. Ablazesphere 10 6,601 Oct-26-2018, 11:41 AM
Last Post: snippsat
  Dice Roller mcmxl22 4 4,073 Feb-05-2018, 09:45 AM
Last Post: buran

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020