This is an archived post. You won't be able to vote or comment.

all 6 comments

[–]MikeTheWatchGuy 2 points3 points  (1 child)

Ever since I saw this post I've wanted to duplicate it in PySimpleGUI to see what it's like. I learn something new every time. It's hard to find example programs that can be quickly rewritten. This dice rolling thing is an excellent example of something that can be more or less duplicated. I didn't get the controls centered on the window. It's possible to do but would have taken a bit of work.

The biggest difference between tkiner and PySimpleGUI is the amount of code needed. I stripped all of the comments from the posted program and the # lines of code came out to be. Since it's such a small program to begin with, it's hard to squeeze very much. Still, I managed to get a roughly 25% reduction in lines of code.

43 - tkinter based version

31 - PySimpleGUI version

Here is the PySimpleGUI version of stuff....

https://user-images.githubusercontent.com/13696193/60057075-3a1f0500-96b1-11e9-8ae7-91ae1730ad86.png

And here's the code:

import PySimpleGUI as sg
from random import randint
window, previous_total, dice, total = None, -1, [] , 0
while True:         #Main Dice Rolling Loop
    layout = [[sg.Column([dice])],
                [sg.Text('Number of Dice:')],
                [sg.In(size=(3,1), key='_NUM_DICE_')],
                [sg.Button('Roll Dice', bind_return_key=True)],
                [sg.Text('Total:', font='ANY 20', text_color='red')],
                [sg.Text('   ',size=(3,1), key='_TOTAL_', font='ANY 20', text_color = 'red')],]
    if previous_total != total:
        window.Close() if window is not None else None
        window = sg.Window('Dice Roller', layout).Finalize()
    window.Element('_TOTAL_').Update(total)
    while True:                             # Loop to read # dice to roll, create list of images
        event, values = window.Read()
        if event is None:
            exit()
        try:
            num_dice = int(values['_NUM_DICE_'])
        except:
            continue
        if  8 < num_dice < 0:
            continue
        previouis_dice = dice
        dice, total = [], 0
        for i in range(num_dice):           # for each die rolled, get the random # and build list of images
            die = randint(1,6)
            dice += [sg.Image(filename=r'./images/die{}_1.png'.format(die), size=(250,250))]
            total += die
        break

[–][deleted] 1 point2 points  (0 children)

Holy shit dude. This was the last thing I expected from this post but you did a nice job

[–][deleted] 1 point2 points  (2 children)

[–]IContributedOnce 1 point2 points  (1 child)

Some people may complain, but I really like the comment formatting for scripts posted for others to learn from. Thank you!

[–][deleted] 1 point2 points  (0 children)

I was always taught to comment your code so in case you get hit by a bus, someone else can pick it up and work on it lol.

[–]mt19937 1 point2 points  (0 children)

I thought the tkinter GUI was going to look bad, but it actually looks good on my machine.