View Full Version : Simple GUI Competition
This comes from a suggestion jemfinch aim'd me during the semester, which I never actually got around to doing. It should be very simple to implement with pretty much any GUI.
Flash Cards
*The goal is to write a program which simulates a set of flashcards.
*The cards simply have one thing written on the front, and something else written on the back. I.e., a set of cards for capitals of states would have a card with "New York" on the front and "Albany" on the back; a set of cards for spanish to english would have "Queso" written on the front and "Cheese" written on the back.
*A set of card should be loaded from a simple text file (the file would just be a set of ordered pairs, whatever format you like), and the order randomized before the user browses them.
*A user should be able to look through the cards, one at a time, and check the back of each card before moving on to the next.
*The user should also be able to browse the cards "backwards", i.e., see "Albany" on the front and "New York" on the back. So they can choose to see capitals and guess states OR see states and guess capitals.
*You can use whatever language and gui toolkit you like, but it should run on windows and linux, and any interpreter should be easily (freely) accessible by anyone who wants to run the program. (You can also feel free to implement this as a web application with CGI if you'd like.)
inkedmn
05-29-2002, 12:31 PM
oh man...
/me cracks open Tk tutorial in "programming python", recalling what a debacle his last attempt at gui turned out to be...
Dru Lee Parsec
05-29-2002, 06:07 PM
Damn you inkedmn! I'm already having a tough enough time trying to find time to write code in the evenings. Now you hit me in my favorite part of Java Coding : G.U.I.!
OK OK OK, I'll stop whining. After all, I started this whole programming challenge thing so I can't b*tch about it. ;)
Hmmm, A GUI flash card system. You know, I was going to put a flash card system in my source forge project.
Hmmmm.
inkedmn
05-29-2002, 06:10 PM
wasn't my idea big daddy!
/me points triumphantly in kmj's general direction... ;)
but i'll give it a go, what the hell!
GnuVince
05-29-2002, 06:15 PM
I don't like GUI's, I suck with GUI's (besides Visual Studio)
inkedmn
05-29-2002, 06:28 PM
Originally posted by GnuVince
...I suck with GUI's (besides Visual Studio)
gnuvince == xilica????
;) j/k
Originally posted by GnuVince
I don't like GUI's, I suck with GUI's (besides Visual Studio)
well it's time to unsuck. :) Qt and Swing are both cooler than visual studio, imo. If you want to do it w/ vc++ (or heck, even vb) feel free. Some people may not be able to play with it though.
Dru: I already commented on irc that this should only take you about a half hour to write. :)
GnuVince
05-29-2002, 06:47 PM
I don't wanna learn Java right now. I don't want to learn QT either. Right now I want to concentrate on a) school b) O'Caml. I've found O'Caml to be a more pleasant language than most languages I've tried in my life. But I think that hand programming a GUI is a pain and I absolutely not enjoy it. And in any case I was reading on another board that some people were not using apps (even though they were faster/better, etc.) that are console-based, so this means that one does not recieves email from 2000 newbies who want to hack the planet with your dict query program.
Dru Lee Parsec
05-29-2002, 07:26 PM
I already commented on irc that this should only take you about a half hour to write
:D Do you remember the One Hour Wumpus I wrote a year or so ago? http://www.brouelette.com/onehourwumpus.html That was quite a challenge. The game "Hunt The Wumpus" in (just a bit over) one hour.
scanez
05-30-2002, 03:59 AM
What, no code yet?!? I've got a java version ready, just have to put it up somewhere- probably a tad too long to post on here. Should have gtk and gtkmm versions soon too.
scanez: (or anyone) if you'd like, you can e-mail me a zip or tar or jar or whatever, and I'll put it on my school account website to make it accessible.
Dru: Yeah; I remember the one-hour wumpus. That's what prompted my half-hour claim for this. :)
vince:
so this means that one does not recieves email from 2000 newbies who want to hack the planet with your dict query program.
..uh, come again? I'm not sure what you mean by this. Generally, the "masses" definitely prefer gui stuff. I like gui stuff too, I'll choose xmms over mpg123 anyday; it's prettier. :)
inkedmn
05-30-2002, 12:13 PM
if anybody needs someplace to put they're code, just email it to me and i'll stick it in inkedmn.net and return the URL where the code can be found.
i've got PLENTY of space and i'm allowed more if i need it...
inkedmn@inkedmn.net
file13
05-30-2002, 12:35 PM
i'll try to whip up a Ada (gtk+) version as soon as i can. but it's a-con this weekend and lots of crap to do in the meantime.... :)
*bump*
...a claw reaches out from the brink of obscurity...
Dru Lee Parsec
06-13-2002, 08:05 PM
You know what? I love this code idea, I just havn't had ANY time at home to work on fun coding projects. I've been working on fun woodworking projects instead.
:(
stuka
06-13-2002, 09:09 PM
Hmmm...sounds like something fun to try - and I've been hunting for an excuse to actually USE Qt (as many of you know, I'm a C++ nut).
file13
06-14-2002, 11:49 AM
ditto. i've got 2 projects i've been working on and i just haven't had time to screw with this. but hopefully soon....
well, I guess I can't complain about it if I haven't even written any code to the effect yet... so I'll try to pull out a python/Tk version this weekend, so expect complaints next week. :)
Dru Lee Parsec
06-21-2002, 08:27 PM
Well folks, I finally got around to writing it in Java using Swing. It's 167 lines long so I'll attach it as a file.
Extract it to a directory that's in your classpath.
Compile with
javac FlashCard.java
To run it type
java FlashCard flashcard.txt
The text file is in the format of
[Question]=[Answer]
In other words write the question, then an equals sign, then the answer all on the same line.
The entire program comes up in it's own frame. The answers pop up inside a little message box. The check box labeled "Display Answers" will show the answers instead of the question. And, the cards are presented in random order.
Hope you enjoy it.
Oh btw, KMJ. It took about 45 minutes.
inkedmn
06-21-2002, 08:33 PM
nice!!!
Dru: nice! I got your code; as soon as I get java set up in linux, I'll check it out.. Hm. Actually, I'll check it out right now, here at work. I've begun work on mine last night, so expect a Python/Tk version sometime this week.
Here's my version. It requires python2.2, but if you change the file(...) function to open(...) it should work with older versions, I think. You also (obviously) have to have the Tkinter libs installed. I would have made the font a bit bigger, but I'm lazy. simply run it with
python2.2 cards.py input.txt
# cards.py a python flashcards program using tkinter
# keith jones - kmj9907@cs.rit.edu (6-18-2002)
# For coderforums.net - should be used with python2.2
from Tkinter import *
import sys
class FlashCard:
def __init__(self, master, bgcolor, width, height, keys, values):
self.canvas = Canvas(master, bg=bgcolor, width=width, height=height)
self.canvas.bind("<Button-1>", self.handle_click)
self.canvas.pack()
self.next_btn = Button(master, text="next", command=self.next_card)
self.next_btn.pack()
self.reverse_btn = Button(master, text="reverse", command=self.reverse)
self.reverse_btn.pack()
self.keys = keys
self.values = values
self.index = 0
self.show_value = 0
self.output = self.canvas.create_text(0,0, text="starting up...")
self.display_message(keys[self.index])
def handle_click(self, event):
if self.show_value == 0:
self.show_value = 1
self.display_message(self.values[self.index])
else:
self.show_value = 0
self.display_message(self.keys[self.index])
def next_card(self, event=None):
self.show_value = 0
self.index += 1
if self.index == len(self.keys):
self.index = 0
self.display_message(self.keys[self.index])
def reverse(self, event=None):
temp = self.keys
self.keys = self.values
self.values = temp
self.display_message(self.keys[self.index])
def display_message(self, msg):
self.canvas.delete(self.output)
self.output = self.canvas.create_text(75, 65, justify="left", text=msg)
class FCApp:
def __init__(self):
self.root = Tk()
infile = file(sys.argv[1])
lines = infile.readlines()
keys = []
vals = []
for line in lines:
if line.find('=') == -1: continue
if line[-1] == '\n': line = line[:-1]
keys.append(line[0:line.find('=')])
vals.append(line[line.find('=')+1:])
self.card = FlashCard(self.root, "white", 250, 150, keys, vals)
self.create_menu()
def create_menu(self):
menu = Menu(self.root)
self.root.config(menu=menu)
filemenu = Menu(menu)
menu.add_cascade(label="File", menu=filemenu)
filemenu.add_command(label="Quit", command=self.quit)
menu.add_command(label="About...", command=self.about)
def quit(self):
self.root.destroy()
def about(self):
self.card.display_message( "FlashCard\n A most basic flashcards\
\n-like application.\n\n Written by keith\n jones for coderforums.net")
def run(self):
mainloop()
program = FCApp()
program.run()
Here's a sample input file... I don't vouche for it's factual correctness:
new york=albany
vermont=montpelier
louisina=baton rouge
alaska=juneau
kentucky=frankfurt
nebraska=lincoln
Try it out. NOW! :)
Strike
06-25-2002, 08:41 AM
kmj: nice, too bad you made me install python2.2-tk to run it ;)
one minor thing that bothers me (and Python coders in general):
from Tkinter import *
You're crapping all over the namespace, man! Just use:
import Tkinter
Also, this would probably be a good candidate for simply inheriting a class from the Tkinter.Canvas class and extending it instead of composing a class with it. But the way you did it was fine, of course.
well, you must have already had 2.2, right? :) so tk couldn't have been much more.
I've always done this the from Tkinter, because I don't think I've ever seen it done any other way. I know that there are some times its not safe to use 'from .. import *', but since this package is always imported this way, I figured it was. I could see how, in a real app, it might be trouble; but in shit apps like this, I'll go for the convenience provided by 'from Tkinter import *'. God knows I forget to specify scope often enough in python.
Re inheriting: yeah, I do think that's a better way. It just so happened the only tkinter code I have which used the canvas was the code I first wrote with Tkinter, so I used that to get something done, even if it may not be aesthetically "the right way". Pretty much every other Tkinter (or other GUI API) proggy I see makes heavy use of inheritence.
Strike
06-25-2002, 12:58 PM
Yeah, with Debian, it's a one-liner to get python-tk, so it's not that big of a deal :)
I must say, yet again, I LUV T3H 4PT!!!11 It has made things soo much easier than any OS I've used. Soo powerful.
scanez
06-26-2002, 11:25 PM
whoops :) Never put my code up...ah well. FYI, it was similar to Dru's, well in looks and what not, little different code :) But tonight for SURE I will whip up a gtkmm version, and maybe gtk+ if time permits, and put it up on my school server :)
scanez
06-27-2002, 04:44 AM
Here we go with the gtkmm version :) You'll need to have the gtkmm devel files installed (package libgtkmm-dev in Debian). Compile with
g++ flash_cards.cpp -o flash_card `gtkmm-config --cflags --libs`
(those are backquotes around gtkmm-config .... --libs of course ;)
Sample text file
Arizona
Phoenix
California
Sacramento
Kansas
Topeka
Washington
Olympia
Idaho
Boise
Iowa
Des Moines
New York
Albany
Nevada
Carson City
Wyoming
Cheyenne
Utah
Salt Lake City
Bugs, fixes?
Edit: Bah, bad spacing, file should be in format:
Question
Answer
Question
Answer
Question
Answer
and should also be named cards.txt
Strike
06-27-2002, 07:01 AM
Hmm, first run seemed to hog a bunch of memory, so I took this "screen shot" to give you an idea of what's happening here:
[ddipaolo@hundley ..rums.net/others]% free -m
total used free shared buffers cached
Mem: 248 30 217 0 1 9
-/+ buffers/cache: 20 228
Swap: 243 28 214
[ddipaolo@hundley ..rums.net/others]% ./flash_cards
zsh: suspended ./flash_cards
[ddipaolo@hundley ..rums.net/others]% free -m
total used free shared buffers cached
Mem: 248 233 15 0 1 17
-/+ buffers/cache: 214 33
Swap: 243 28 214
Now, to look at the code and see what is doing that.
Strike
06-27-2002, 07:02 AM
Actually, I let it run a bit more, and perhaps this line from top is a bit more telling:
PID USER PRI NI SIZE RSS SHARE STAT %CPU %MEM TIME COMMAND
3618 ddipaolo 9 0 327M 150M 2272 T 0.0 60.3 0:24 flash_cards
327MB, eh? You calculating primes with this too?
Strike
06-27-2002, 07:04 AM
Oh, that was without a cards.txt, btw. I just ran it without creating one to see what it'd do. I'll try and diagnose it now. Works fine with a cards.txt.
Strike
06-27-2002, 07:14 AM
Ah, you never check if the file object gets created. Throw this in before the while loop:
if (file == NULL) {
return 1;
}
and all will be well. :)
Oh, and what was making the memory run out of control was the fact that you were making a bunch of blank FlashCard objects and appending them to the cards vector.
scanez
06-27-2002, 02:09 PM
eek! Damn that is a lot of memory, I just ran it without cards.txt too and boy was that sucker growing big. Anyway, check for file has been added :)
gufmn
06-27-2002, 02:43 PM
Originally posted by inkedmn
[B]if anybody needs someplace to put they're code, just email it to me and i'll stick it in inkedmn.net and return the URL where the code can be found.
i've got PLENTY of space and i'm allowed more if i need it...
umm.....sure offer space to anyone in the world. after all, i'm the one paying the hosting fee ;)
scanez
06-27-2002, 03:02 PM
Originally posted by gufmn
umm.....sure offer space to anyone in the world. after all, i'm the one paying the hosting fee ;)
lol
scanez
06-27-2002, 04:37 PM
And now of course the gtk+ version. Must have the gtk+ development files installed (libgtk1.2-dev in Debian). Same format for input file, only this time the filename is given on command line so use whatever name you see fit :) Compile with
gcc flash_cards.c -o flash_cards `gtk-config --cflags --libs`
Edit: Oh yeah, and can only have (and must have) 10 question/answer pairs at the moment.
inkedmn
06-28-2002, 05:27 AM
Originally posted by gufmn
umm.....sure offer space to anyone in the world. after all, i'm the one paying the hosting fee ;)
yeah, well you're ugly :)
Strike
06-28-2002, 09:33 AM
"Madam, I may be drunk, but you're ugly. And tomorrow, I will be sober."
stuka
07-05-2002, 01:09 PM
Well, I wrote this up in C++ using Qt yesterday, and was going to post it here from work today (as my #$%!!*&^! phones are STILL out, thank you Verizon....) but I grabbed the wrong disk. Verizon is supposed to fix my phones today, so if they're working tonight I'll put it up here - it's not perfect (I don't like the architecture from an OO standpoint) and it has an annoying semi-bug when you go from hitting 'next' to 'previous', which I KNOW has to do with how I'm handling my list iterators, but my brain was giving up on me last night. On the other hand, it works, and the file format is simple (and the reader can handle malformed lines, and comments, but I haven't REALLY tested it). So, I'll throw it up here when I get a chance.
stuka
07-10-2002, 01:27 AM
OK, here it is - as warned, it ain't perfect. Maybe I'll get the time to fix it up later. The tarball has a Makefile and an example data file, so it should be easy to get going.
<edit & bump>
Well, I finally, after a looooong delay, fixed this right, and made a makefile for it, and all that stuff. It's a much better example of Qt GUI programming (and C++ design, I think).
</edit & bump>
stuka
12-29-2002, 02:04 AM
OK, shameless bump! (But for a reason ;) )
Strike
12-29-2002, 11:13 AM
Er, where is it?
stuka
12-30-2002, 11:15 AM
Doh! It didn't attach! I'll have to fix that tonight when I get home.
stuka
01-03-2003, 12:46 AM
OK, if this don't make it I'll be peeved.
gufmn
01-03-2003, 01:41 PM
Originally posted by Stuka
OK, shameless bump! (But for a reason ;) )
Kinda wierd that this thread got active again, I just stumbled across it the other day and have started a java version. It's nearly finished, just gotta work out a JDialog for inputing new cards.
Flangazor
09-25-2003, 12:08 PM
Swing is not cool at all. Qt, on the other hand, is.
vBulletin® v3.7.0, Copyright ©2000-2009, Jelsoft Enterprises Ltd.