4
\$\begingroup\$

I am doing some simple projects in an attempt to get good at programming, this is my first GUI would love some feedback, and some guidelines.

from tkinter import *
import random

root = Tk()
root.resizable(False, False)

root.title('Coinflipper')

topframe = Frame(root)
topframe.pack()
botframe = Frame(root)
botframe.pack(side=BOTTOM)
midframe = Frame(root)
midframe.pack()

choice = Label(topframe, text="Enter the number of flips: ")
choice.grid(row=1)
ent = Entry(topframe)
ent.grid(row=1, column=2)

clickit = Button(botframe, text="FLIP THE COIN!!!")
clickit.pack()

out = Text(midframe, width=15, height=1)
out2 = Text(midframe, width=15, height=1)
out.grid(row=1, column=1, columnspan=3)
out2.grid(row=2, column=1, columnspan=3)


def flipy(event):
    guess = ent.get()
    heads = []
    tails = []

    if guess == '' or guess == str(guess):
        out.delete(1.0, "end-1c")
        out.insert("end-1c", 'Invalid')

    for flips in range(int(guess)):
        out.delete(1.0, "end-1c")
        out2.delete(1.0, "end-1c")
        random_number = random.randint(1, 2)
        if random_number == 1:
            heads.append("Heads")
        elif random_number == 2:
            tails.append("Tails")

    out.insert("end-1c", len(tails))
    out.insert("end-1c", " -TAILS")
    out2.insert("end-1c", len(heads))
    out2.insert("end-1c", " -HEADS")


clickit.bind("<Button-1>", flipy)

root.mainloop()
\$\endgroup\$

2 Answers 2

3
\$\begingroup\$

You are abusing the heads & tails lists into making simple counters.

heads = []
tails = []

for flips in range(int(guess)):
    random_number = random.randint(1, 2)
    if random_number == 1:
        heads.append("Heads")
    elif random_number == 2:
        tails.append("Tails")

len(tails)
len(heads)

This could be replaced with simply:

heads = 0
tails = 0

for flips in range(int(guess)):
    random_number = random.randint(1, 2)
    if random_number == 1:
        heads += 1
    else:
        tails += 1
\$\endgroup\$
1
  • \$\begingroup\$ Thank you ! I tend to overcomplicate things for myself. Guess that will go away with practice. \$\endgroup\$
    – skyn37
    Nov 26, 2018 at 21:21
0
\$\begingroup\$

Skyn37,

This a great first time GUI setup. Only saw a few things I would change to make it easier on yourself. I don't see anything wrong with your Function.

Things that need work:

1.Line 9 to 14: These are unnecessary as you already started using grid. Yet this is good practice. Just not needed for this program.

2.Also try to organize your code better. It helps yourself and also others to quickly identify what the code is doing. I made a few tweaks to your code. Organized it a bit, and made a few minor changes. Placed everything using Grid and Tidied up the GUI a bit.

"""Geometry and Title"""
root.title('Coinflipper')
root.geometry("300x100")
root.resizable(False,False)

"""Labels & Text Box"""
choice = Label(text="How Many Flips: ")
T = Label(text="Tails: ")
H = Label(text="Heads: ")
ent = Entry(root)
out = Text(width=15, height=1)
out2 = Text(width=15, height=1)

"""Grid Positions"""
choice.grid(row=0, column=0, sticky=E)
T.grid(row=1,column=0,sticky=E)
H.grid(row=2,column=0,sticky=E)
ent.grid(row=0, column=1, columnspan=3)
out.grid(row=1, column=1, columnspan=3)
out2.grid(row=2, column=1, columnspan=3)

"""Button Text and Position"""
clickit = Button(text="FLIP THE COIN!!!")
clickit.grid(row=3,column=1)

Overall great work!

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.