1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465
|
"""
.. moduleauthor:: easygui developers and Stephen Raymond Ferg
.. default-domain:: py
.. highlight:: python
"""
import os
import sys
try:
from . import utils as ut
from .button_box import buttonbox
from .text_box import textbox
from .diropen_box import diropenbox
from .fileopen_box import fileopenbox
from .filesave_box import filesavebox
from .multi_fillable_box import multenterbox
from .multi_fillable_box import multpasswordbox
from .derived_boxes import ynbox
from .derived_boxes import ccbox
from .derived_boxes import boolbox
from .derived_boxes import indexbox
from .derived_boxes import msgbox
from .derived_boxes import integerbox
from .derived_boxes import enterbox
from .derived_boxes import exceptionbox
from .derived_boxes import codebox
from .derived_boxes import passwordbox
from .choice_box import choicebox
from .choice_box import multchoicebox
from . import about
from .about import eg_version
from .about import abouteasygui
except (SystemError, ValueError, ImportError):
print("Please run demo.py from outside the package")
exit()
# --------------------------------------------------------------
#
# test/demo easygui
#
# -----------------------------------------------------------------------
package_dir = os.path.dirname(os.path.dirname(os.path.realpath(__file__)))
class Demos(object):
""" Collection of demos
A choice is comprised of two pieces of data:
- a description, which is a string. The descriptions will be shown
in the choicebox, and one will be returned by it.
- a function to execute when the description is selected
"""
def __init__(self):
self.demos = [
("msgbox", demo_msgbox),
("ynbox", demo_ynbox),
("ccbox", demo_ccbox),
("boolbox", demo_boolbox),
("buttonbox", demo_buttonbox),
("buttonbox that displays an image", demo_buttonbox_with_image),
("buttonbox - select an image", demo_buttonbox_with_choice),
("indexbox", demo_indexbox),
("choicebox", demo_choicebox),
("multchoicebox", demo_multichoicebox),
("textbox", demo_textbox),
("codebox", demo_codebox),
("enterbox", demo_enterbox),
("integerbox", demo_integerbox),
("passwordbox", demo_passwordbox),
("multenterbox", demo_multenterbox),
("multpasswordbox", demo_multpasswordbox),
("enterbox that displays an image", demo_enterbox_image),
("filesavebox", demo_filesavebox),
("fileopenbox", demo_fileopenbox),
("diropenbox", demo_diropenbox),
("exceptionbox", demo_exceptionbox),
("About EasyGui", demo_about),
("Help", demo_help),
]
def list_descriptions(self):
descriptions = [c[0] for c in self.demos]
return descriptions
def get_demo(self, index):
demo = self.demos[index]
# demo = next((c[1] for c in self.demo if c[0] == description))
return demo[1]
def get_description(self, index):
demo = self.demos[index]
return demo[0]
def __len__(self):
return len(self.demos)
def easygui_demo():
"""
Run the EasyGui demo.
"""
demos = Demos()
replies = [''] * len(demos)
# clear the console
print('\n' * 100)
msg = []
msg.append("Pick the kind of box that you wish to demo.")
msg.append(" * Python version {}".format(sys.version))
msg.append(" * EasyGui version {}".format(eg_version))
msg.append(" * Tk version {}".format(ut.TkVersion))
intro_message = "\n".join(msg)
title = "EasyGui " + eg_version
# Table that relates keys in choicebox with functions to execute
descriptions = demos.list_descriptions()
preselected = 0
while True:
presented_choices = [
d + r for d, r in zip(descriptions, replies)]
reply = choicebox(msg=intro_message,
title=title,
choices=presented_choices,
preselect=preselected
)
if not reply:
break
print(reply)
index_chosen_demo = presented_choices.index(reply)
# Execute the chosen demo!
chosen_demo = demos.get_demo(index_chosen_demo)
demo_reply = chosen_demo()
# Save the reply
if demo_reply:
replies[index_chosen_demo] = ' - Last reply: {}'.format(demo_reply)
else:
replies[index_chosen_demo] = ''
preselected += 1
if preselected >= len(presented_choices):
preselected = 0
def demo_msgbox():
reply = msgbox("short msg", "This is a long title")
print("Reply was: {!r}".format(reply))
return reply
def demo_buttonbox():
reply = buttonbox(choices=['one', 'two', 'two', 'three'],
default_choice='two')
print("Reply was: {!r}".format(reply))
title = "Demo of Buttonbox with many, many buttons!"
msg = ("This buttonbox shows what happens when you "
"specify too many buttons.")
reply = buttonbox(msg=msg, title=title,
choices=['1', '2', '3', '4', '5', '6', '7'],
cancel_choice='7')
print("Reply was: {!r}".format(reply))
return reply
def demo_buttonbox_with_image():
msg = "Do you like this picture?\nIt is "
choices = ["Yes", "No", "No opinion"]
for image in [
os.path.join(package_dir, "python_and_check_logo.gif"),
os.path.join(package_dir, "python_and_check_logo.jpg"),
os.path.join(package_dir, "python_and_check_logo.png"),
os.path.join(package_dir, "zzzzz.gif")]:
reply = buttonbox(msg + image, image=image, choices=choices)
print("Reply was: {!r}".format(reply))
return reply
def demo_buttonbox_with_choice():
msg = "Pick an image"
choices = ['cancel']
images = list()
images.append(os.path.join(package_dir, "python_and_check_logo.gif"))
images.append(os.path.join(package_dir, "python_and_check_logo.jpg"))
images.append(os.path.join(package_dir, "python_and_check_logo.png"))
images.append(os.path.join(package_dir, "zzzzz.gif"))
reply = buttonbox(msg, images=images, choices=['cancel'])
print("Reply was: {!r}".format(reply))
return reply
def demo_ccbox():
msg = "Insert your favorite message here"
title = "Demo of ccbox"
reply = ccbox(msg, title)
print("Reply was: {!r}".format(reply))
return reply
def demo_multichoicebox():
listChoices = ["aaa", "bbb", "ccc", "ggg", "hhh", "iii",
"jjj", "kkk", "LLL", "mmm", "nnn", "ooo",
"ppp", "qqq", "rrr", "sss", "ttt", "uuu",
"vvv"]
preselect = None #[0, 2, 4]
msg = "Pick as many choices as you wish."
reply = multchoicebox(msg, "Demo of multchoicebox", listChoices, preselect)
print("Reply was: {!r}".format(reply))
return reply
def demo_ynbox():
title = "Demo of ynbox"
msg = "Were you expecting the Spanish Inquisition?"
reply = ynbox(msg, title)
print("Reply was: {!r}".format(reply))
if reply:
msgbox("NOBODY expects the Spanish Inquisition!", "Wrong!")
return reply
def demo_choicebox():
title = "Demo of choicebox"
longchoice = (
"This is an example of a very long option "
"which you may or may not wish to choose."
* 2)
listChoices = ["nnn", "ddd", "eee", "fff", "aaa",
longchoice, "aaa", "bbb", "ccc", "ggg", "hhh",
"iii", "jjj", "kkk", "LLL", "mmm", "nnn",
"ooo", "ppp", "qqq",
"rrr", "sss", "ttt", "uuu", "vvv"]
msg = ("Pick something. " +
("A wrapable sentence of text ?! " * 30) +
"\nA separate line of text." * 6)
reply = choicebox(msg=msg, choices=listChoices)
print("Reply was: {!r}".format(reply))
msg = "Pick something. "
reply = choicebox(msg=msg, title=title, choices=listChoices)
print("Reply was: {!r}".format(reply))
msg = "Pick something. "
reply = choicebox(
msg="The list of choices is empty!", choices=list())
print("Reply was: {!r}".format(reply))
return reply
def demo_integerbox():
reply = integerbox(
"Enter a number between 3 and 333",
"Demo: integerbox WITH a default value", 222, 3, 333)
print("Reply was: {!r}".format(reply))
reply = integerbox(
"Enter a number between 0 and 99",
"Demo: integerbox WITHOUT a default value"
)
print("Reply was: {!r}".format(reply))
return reply
def demo_about():
reply = abouteasygui()
print("Reply was: {!r}".format(reply))
return reply
def demo_enterbox():
image = os.path.join(package_dir, "python_and_check_logo.gif")
message = ("Enter the name of your best friend."
"\n(Result will be stripped.)")
reply = enterbox(message, "Love!", " Suzy Smith ")
print("Reply was: {!r}".format(reply))
message = ("Enter the name of your best friend."
"\n(Result will NOT be stripped.)")
reply = enterbox(
message, "Love!", " Suzy Smith ", strip=False)
print("Reply was: {!r}".format(reply))
reply = enterbox("Enter the name of your worst enemy:", "Hate!")
print("Reply was: {!r}".format(reply))
return reply
def demo_multpasswordbox():
msg = "Enter logon information"
title = "Demo of multpasswordbox"
fieldNames = ["Server ID", "User ID", "Password"]
fieldValues = list() # we start with blanks for the values
fieldValues = multpasswordbox(msg, title, fieldNames)
# make sure that none of the fields was left blank
while True:
if fieldValues is None:
break
errs = list()
for n, v in zip(fieldNames, fieldValues):
if v.strip() == "":
errs.append('"{}" is a required field.\n\n'.format(n))
if not len(errs):
break # no problems found
fieldValues = multpasswordbox(
"".join(errs), title, fieldNames, fieldValues)
print("Reply was: {!s}".format(fieldValues))
return fieldValues
def demo_textbox():
text_snippet = ((
"It was the best of times, and it was the worst of times. The rich "
"ate cake, and the poor had cake recommended to them, but wished "
"only for enough cash to buy bread. The time was ripe for "
"revolution! "
* 5) + "\n\n") * 10
title = "Demo of textbox"
msg = "Here is some sample text. " * 16
reply = textbox(msg, title, text_snippet)
print("Reply was: {!s}".format(reply))
return reply
def demo_codebox():
# TODO RL: Turn this sample code into the code in this module, just for fun
code_snippet = ("dafsdfa dasflkj pp[oadsij asdfp;ij asdfpjkop asdfpok asdfpok asdfpok" * 3) + "\n" + """# here is some dummy Python code
for someItem in myListOfStuff:
do something(someItem)
do something()
do something()
if somethingElse(someItem):
doSomethingEvenMoreInteresting()
""" * 16
msg = "Here is some sample code. " * 16
reply = codebox(msg, "Code Sample", code_snippet)
print("Reply was: {!r}".format(reply))
return reply
def demo_boolbox():
reply = boolbox()
print("Reply was: {!r}".format(reply))
return reply
def demo_enterbox_image():
image = os.path.join(package_dir, "python_and_check_logo.gif")
message = "What kind of snake is this?"
reply = enterbox(message, "Quiz", image=image)
print("Reply was: {!r}".format(reply))
return reply
def demo_passwordbox():
reply = passwordbox("Demo of password box WITHOUT default"
+ "\n\nEnter your secret password",
"Member Logon")
print("Reply was: {!s}".format(reply))
reply = passwordbox("Demo of password box WITH default"
+ "\n\nEnter your secret password",
"Member Logon", "alfie")
print("Reply was: {!s}".format(reply))
return reply
def demo_help():
codebox("EasyGui Help", text=about.EASYGUI_ABOUT_INFORMATION)
return None
def demo_filesavebox():
filename = "myNewFile.txt"
title = "File SaveAs"
msg = "Save file as:"
f = filesavebox(msg, title, default=filename)
print("You chose to save file: {}".format(f))
return f
def demo_diropenbox():
title = "Demo of diropenbox"
msg = "Pick the directory that you wish to open."
d = diropenbox(msg, title)
print("You chose directory...: {}".format(d))
d = diropenbox(msg, title, default="./")
print("You chose directory...: {}".format(d))
d = diropenbox(msg, title, default="c:/")
print("You chose directory...: {}".format(d))
return d
def demo_exceptionbox():
try:
thisWillCauseADivideByZeroException = 1 / 0
except:
exceptionbox()
return None
def demo_indexbox():
title = "Indexbox"
msg = "Demo of " + "indexbox"
choices = ["Choice1", "Choice2", "Choice3", "Choice4"]
reply = indexbox(msg, title, choices)
print("Reply was: {!r}".format(reply))
return reply
def demo_multenterbox():
msg = "Enter your personal information"
title = "Credit Card Application"
fieldNames = ["Name", "Street Address", "City", "State", "ZipCode"]
fieldValues = list() # we start with blanks for the values
fieldValues = multenterbox(msg, title, fieldNames)
# make sure that none of the fields was left blank
while True:
if fieldValues is None:
break
errs = list()
for n, v in zip(fieldNames, fieldValues):
if v.strip() == "":
errs.append('"{}" is a required field.'.format(n))
if not len(errs):
break # no problems found
fieldValues = multenterbox(
"\n".join(errs), title, fieldNames, fieldValues)
print("Reply was: {}".format(fieldValues))
return fieldValues
def demo_fileopenbox():
msg = "Python files"
title = "Open files"
default = "*.py"
f = fileopenbox(msg, title, default=default)
print("You chose to open file: {}".format(f))
default = "./*.gif"
msg = "Some other file types (Multi-select)"
filetypes = ["*.jpg", ["*.zip", "*.tgs", "*.gz",
"Archive files"], ["*.htm", "*.html", "HTML files"]]
f = fileopenbox(
msg, title, default=default, filetypes=filetypes, multiple=True)
print("You chose to open file: %s" % f)
return f
|