Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 163 lines (123 sloc) 4.731 kb
3ed81b0 Paul Goins Initial commit.
authored
1 #!/usr/bin/env python
2 # -*- coding: utf-8 -*-
3
533ae8b Paul Goins Added license docstring.
authored
4 """
ab6c0e4 Paul Goins Finished updating README and flashcards.py.
authored
5 Flash Cards
533ae8b Paul Goins Added license docstring.
authored
6
7 Author: Paul Goins <general@vultaire.net>
8 License: GNU GPL v2 or later
9
10 """
11
3ed81b0 Paul Goins Initial commit.
authored
12 from Tkinter import Tk, Frame, Label
867937d Paul Goins Added command line options, removed dummy data.
authored
13 import argparse, os, gettext, random
ed7ce6f Paul Goins Replaced remaining references to "kanji".
authored
14 gettext.install("net.vultaire.flashcards")
3ed81b0 Paul Goins Initial commit.
authored
15
ed7ce6f Paul Goins Replaced remaining references to "kanji".
authored
16 PROGRAM_NAME=_("Flash Cards")
26cf993 Paul Goins Added on-top and version options, updated README.
authored
17 VERSION="0.2"
18
3ed81b0 Paul Goins Initial commit.
authored
19
20 class EmptyList(Exception):
21 pass
22
23
24 class MainWindow(object):
25
26cf993 Paul Goins Added on-top and version options, updated README.
authored
26 def __init__(self, root, data, interval, font_face, font_size, on_top):
3ed81b0 Paul Goins Initial commit.
authored
27 # GUI display
28 self.root = root
29 frame = Frame(root)
30 frame.pack()
31
867937d Paul Goins Added command line options, removed dummy data.
authored
32 font = (font_face, font_size)
45927a0 Paul Goins Renamed on_click() to flip_card(). Replaced "kanji" references with "ca...
authored
33 self.card = Label(frame, font=font)
34 self.card.pack()
3ed81b0 Paul Goins Initial commit.
authored
35
45927a0 Paul Goins Renamed on_click() to flip_card(). Replaced "kanji" references with "ca...
authored
36 # Card refresh interval, in seconds
3ed81b0 Paul Goins Initial commit.
authored
37 self.refresh_interval = interval
38
39 # Data for tracking
40 self.data = data
41 self.pending = []
42 self.current = None
43
45927a0 Paul Goins Renamed on_click() to flip_card(). Replaced "kanji" references with "ca...
authored
44 # Update card tracking
45 self.reset_card()
46 self.update_card()
3ed81b0 Paul Goins Initial commit.
authored
47
26cf993 Paul Goins Added on-top and version options, updated README.
authored
48 if on_top:
49 self.root.wm_attributes("-topmost", 1)
9797421 Paul Goins Added always-on-top support for Windows.
authored
50
f11b3c5 Paul Goins Moved click event to root window. Added Enter as equivalent event.
authored
51 # Bind events
a335555 Paul Goins Added backward flipping and one more key for forward flipping. Document...
authored
52 self.root.bind("<Button-1>", self.flip_forward)
53 self.root.bind("<Key-Return>", self.flip_forward)
54 self.root.bind("<Key-space>", self.flip_forward)
55
56 self.root.bind("<Button-3>", self.flip_backward)
57 self.root.bind("<Key-BackSpace>", self.flip_backward)
f11b3c5 Paul Goins Moved click event to root window. Added Enter as equivalent event.
authored
58
45927a0 Paul Goins Renamed on_click() to flip_card(). Replaced "kanji" references with "ca...
authored
59 def reset_card(self):
3ed81b0 Paul Goins Initial commit.
authored
60 """
45927a0 Paul Goins Renamed on_click() to flip_card(). Replaced "kanji" references with "ca...
authored
61 Resets the pending list of cards.
3ed81b0 Paul Goins Initial commit.
authored
62
63 This method will automatically filter out duplicate entries.
64
65 """
66 self.pending = list(set(self.data[:]))
67
45927a0 Paul Goins Renamed on_click() to flip_card(). Replaced "kanji" references with "ca...
authored
68 def pull_next_card(self):
3ed81b0 Paul Goins Initial commit.
authored
69 """
45927a0 Paul Goins Renamed on_click() to flip_card(). Replaced "kanji" references with "ca...
authored
70 Pulls the next card from the card list.
3ed81b0 Paul Goins Initial commit.
authored
71
45927a0 Paul Goins Renamed on_click() to flip_card(). Replaced "kanji" references with "ca...
authored
72 This method tracks the current card and will avoid pulling
73 the same card two times in a row (unless there's only one
74 card left to pull).
3ed81b0 Paul Goins Initial commit.
authored
75
45927a0 Paul Goins Renamed on_click() to flip_card(). Replaced "kanji" references with "ca...
authored
76 Returns the next card. The value is also set as
3ed81b0 Paul Goins Initial commit.
authored
77 self.current.
78
79 """
45927a0 Paul Goins Renamed on_click() to flip_card(). Replaced "kanji" references with "ca...
authored
80 card = self.current
3ed81b0 Paul Goins Initial commit.
authored
81 if len(self.pending) < 1:
82 raise EmptyList()
83 elif len(self.pending) == 1:
84 index = 0
45927a0 Paul Goins Renamed on_click() to flip_card(). Replaced "kanji" references with "ca...
authored
85 card = self.pending[0]
3ed81b0 Paul Goins Initial commit.
authored
86 else:
45927a0 Paul Goins Renamed on_click() to flip_card(). Replaced "kanji" references with "ca...
authored
87 while card == self.current:
3ed81b0 Paul Goins Initial commit.
authored
88 index = random.randint(0, len(self.pending) - 1)
45927a0 Paul Goins Renamed on_click() to flip_card(). Replaced "kanji" references with "ca...
authored
89 card = self.pending[index]
3ed81b0 Paul Goins Initial commit.
authored
90
91 del self.pending[index]
92
93 if len(self.pending) < 1:
45927a0 Paul Goins Renamed on_click() to flip_card(). Replaced "kanji" references with "ca...
authored
94 self.reset_card()
3ed81b0 Paul Goins Initial commit.
authored
95
45927a0 Paul Goins Renamed on_click() to flip_card(). Replaced "kanji" references with "ca...
authored
96 self.current = card
ac673fc Paul Goins Now supports flippable cards via csv syntax; click on window to flip.
authored
97 self.current_index = 0
45927a0 Paul Goins Renamed on_click() to flip_card(). Replaced "kanji" references with "ca...
authored
98 return card
3ed81b0 Paul Goins Initial commit.
authored
99
45927a0 Paul Goins Renamed on_click() to flip_card(). Replaced "kanji" references with "ca...
authored
100 def update_card(self):
3ed81b0 Paul Goins Initial commit.
authored
101 """
45927a0 Paul Goins Renamed on_click() to flip_card(). Replaced "kanji" references with "ca...
authored
102 Pulls the next card and updates the GUI.
3ed81b0 Paul Goins Initial commit.
authored
103
104 This is a self-recurring call. It should only be called at
105 the beginning of the application.
106
107 """
45927a0 Paul Goins Renamed on_click() to flip_card(). Replaced "kanji" references with "ca...
authored
108 card = self.pull_next_card()
109 self.card.configure(text=card[0])
3ed81b0 Paul Goins Initial commit.
authored
110
111 refresh_ms = int(self.refresh_interval * 1000)
45927a0 Paul Goins Renamed on_click() to flip_card(). Replaced "kanji" references with "ca...
authored
112 self.root.after(refresh_ms, self.update_card)
3ed81b0 Paul Goins Initial commit.
authored
113
a335555 Paul Goins Added backward flipping and one more key for forward flipping. Document...
authored
114 def flip_forward(self, event):
ac673fc Paul Goins Now supports flippable cards via csv syntax; click on window to flip.
authored
115 self.current_index = (self.current_index + 1) % len(self.current)
45927a0 Paul Goins Renamed on_click() to flip_card(). Replaced "kanji" references with "ca...
authored
116 self.card.configure(text=self.current[self.current_index])
ac673fc Paul Goins Now supports flippable cards via csv syntax; click on window to flip.
authored
117
a335555 Paul Goins Added backward flipping and one more key for forward flipping. Document...
authored
118 def flip_backward(self, event):
119 self.current_index = (self.current_index - 1) % len(self.current)
120 self.card.configure(text=self.current[self.current_index])
3ed81b0 Paul Goins Initial commit.
authored
121
867937d Paul Goins Added command line options, removed dummy data.
authored
122 def parse_args():
26cf993 Paul Goins Added on-top and version options, updated README.
authored
123 ap = argparse.ArgumentParser(
124 description=_("Randomly displays characters or words in a window, "
125 "refreshing every 30 seconds or so."))
867937d Paul Goins Added command line options, removed dummy data.
authored
126 ap.add_argument("-f", "--font-face", default=None,
127 help=_("Specify font face."))
128 ap.add_argument("-s", "--font-size", type=int, default=100,
129 help=_("Specify font size in points. (Default: %(default)s)"))
130 ap.add_argument("-i", "--interval", type=int, default=30,
131 help=_("Specify how long to wait before changing entries. "
132 "(Default: %(default)s)"))
26cf993 Paul Goins Added on-top and version options, updated README.
authored
133 ap.add_argument("-t", "--on-top", action="store_true", default=False,
134 help=_("Make the window stay always on top."))
135 ap.add_argument("-v", "--version", action="store_true", default=False,
136 help=_("Show version and exit."))
867937d Paul Goins Added command line options, removed dummy data.
authored
137 ap.add_argument("filename",
138 help=_("A UTF-8 encoded file, containing one line per character "
139 "or word for review."))
140 return ap.parse_args()
141
3ed81b0 Paul Goins Initial commit.
authored
142 def main():
ac673fc Paul Goins Now supports flippable cards via csv syntax; click on window to flip.
authored
143 import sys, csv
3ed81b0 Paul Goins Initial commit.
authored
144
867937d Paul Goins Added command line options, removed dummy data.
authored
145 options = parse_args()
26cf993 Paul Goins Added on-top and version options, updated README.
authored
146 if options.version:
147 print "%s v%s" % (PROGRAM_NAME, VERSION)
148 sys.exit(0)
867937d Paul Goins Added command line options, removed dummy data.
authored
149
150 with open(options.filename) as infile:
ac673fc Paul Goins Now supports flippable cards via csv syntax; click on window to flip.
authored
151 reader = csv.reader(infile)
152 data = [tuple([col.decode("utf-8") for col in row])
ad91c33 Paul Goins Fixed empty line case.
authored
153 for row in reader if len(row) > 0]
3ed81b0 Paul Goins Initial commit.
authored
154
155 root = Tk()
ed7ce6f Paul Goins Replaced remaining references to "kanji".
authored
156 root.title(PROGRAM_NAME)
867937d Paul Goins Added command line options, removed dummy data.
authored
157 window = MainWindow(root, data, options.interval, options.font_face,
26cf993 Paul Goins Added on-top and version options, updated README.
authored
158 options.font_size, options.on_top)
3ed81b0 Paul Goins Initial commit.
authored
159 root.mainloop()
160
161 if __name__ == "__main__":
162 main()
Something went wrong with that request. Please try again.