Support Forums

Full Version: My first GUI
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2
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.

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()
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 Blackhat

EDIT: fixed some errors in the source code.
Screeny Big Grin?
sure one sec


Edit:
Mm looks interesting, I will try it ;).
Good job man. Shame I keep losing Sad
(01-02-2010, 07:24 PM)Holy Wrote: [ -> ]Good job man. Shame I keep losing Sad
Yeah it gets kinda old after like 5 seconds. I was just testing out different functions. :p
Its nice, ill mite try and use your source to make my own GUI programs if ya dont mind.
(01-04-2010, 03:19 AM)Fail Ninja Wrote: [ -> ]Its nice, ill mite try and use your source to make my own GUI programs if ya dont mind.

not at all Blackhat
I don't mind Tkinter, but I suggest you to start early with WX or QT, I heard wx is c0ol to use and all, but I'm using QT..
Other than that nice job!
(01-05-2010, 01:30 AM)Master of The Universe Wrote: [ -> ]I don't mind Tkinter, but I suggest you to start early with WX or QT, I heard wx is c0ol to use and all, but I'm using QT..
Other than that nice job!

Yeah I know, every time i ask fallen for Tkinter help, he just says wxPython is better! And i agree with him, its just my book covers Tkinter, so im starting with that.
Pages: 1 2