01-02-2010, 02:22 PM
Well here is my first GUI(gooey :p). It's a "game" technically speaking , although a really crappy game. Just testing out buttons and updating Entry fields. It isn't very neat and well I think I did some unnecessary things, but I just wanted to make sure it would work. Basically you press one of three buttons, and after you press a button, the round changes, and you money either goes up 100 or down 100 whether you win or lose. Pretty stupid but again, I was testing things out.
If there is anything I can do to make the code neater or shorter please tell me as I am new at GUI's and Classes. If there is anything I can do to improve the "game" please let me know. Enjoy
EDIT: fixed some errors in the source code.
Code:
from Tkinter import *
from tkMessageBox import showinfo
import random
money = 0
round = 0
class gooey(Frame):
def __init__(self, parent=None):
global money
global e
global r
global round
Frame.__init__(self, parent)
Label(text='Money').pack(side=BOTTOM)
e = Entry(money)
e.pack(side=BOTTOM)
Label(text='Round').pack(side=BOTTOM)
r = Entry(round)
r.pack(side=BOTTOM)
button1 = Button(self, text='Press me', command=self.btn1)
button1.pack()
button2 = Button(self, text='Press me', command=self.btn2)
button2.pack(side=RIGHT)
button3 = Button(self, text='Press me', command=self.btn3)
button3.pack(side=LEFT)
def btn1(self):
global money
global e
global r
global round
a = 'You win, Congrats!'
b = 'You lose, Sorry :('
c = [a , b]
d = random.choice(c)
showinfo(title='Result', message=d)
if d==a:
money += 100
e.delete(0, END)
e.insert(0, money)
round += 1
r.delete(0, END)
r.insert(0, round)
self
if d==b:
money -=100
e.delete(0, END)
e.insert(0, money)
round +=1
r.delete(0, END)
r.insert(0, round)
self
def btn2(self):
global money
global e
global r
global round
a = 'You win, Congrats!'
b = 'You lose, Sorry :('
c = [a , b]
d = random.choice(c)
showinfo(title='Result', message=d)
if d==a:
money += 100
e.delete(0, END)
e.insert(0, money)
round += 1
r.delete(0, END)
r.insert(0, round)
self
if d==b:
money -=100
e.delete(0, END)
e.insert(0, money)
round += 1
r.delete(0, END)
r.insert(0, round)
self
def btn3(self):
global money
global e
global r
global round
a = 'You win, Congrats!'
b = 'You lose, Sorry :('
c = [a , b]
d = random.choice(c)
showinfo(title='Result', message=d)
if d==a:
money += 100
e.delete(0, END)
e.insert(0, money)
round += 1
r.delete(0, END)
r.insert(0, round)
self
if d==b:
money -=100
e.delete(0, END)
e.insert(0, money)
round += 1
r.delete(0, END)
r.insert(0, round)
self
if __name__ == '__main__':
window = gooey()
window.pack()
window.mainloop()
EDIT: fixed some errors in the source code.