Advertisement
Guest User

PickRandom2.py

a guest
Nov 25th, 2014
1,247
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.17 KB | None | 0 0
  1. # -*- coding: utf-8 -*-
  2. import os
  3. import random
  4. from tkinter import *
  5. from tkinter import ttk
  6. root = Tk()
  7. path = os.getcwd()
  8. lst = os.listdir(path)
  9. text = StringVar()
  10. text.set('I choose nothing. Yet...')
  11.  
  12.  
  13. def PickRandom(lst):
  14.     pick = random.choice(lst)
  15.     text.set('I choose this:\n' + pick)
  16.     print(pick)
  17.     return pick
  18.  
  19.  
  20. def Delete():  
  21.     t = text.get().split('\n')[1]
  22.     if os.path.isfile(t):
  23.         os.remove(t)
  24.     else:
  25.         try:
  26.             os.removedirs(t)
  27.         except OSError as e:
  28.             l = os.listdir(t)
  29.             print('l: ', l)
  30.             for i in l:
  31.                 print('i: %s\\%s - is file: %s; is dir: %s' %
  32.                       (t, i, os.path.isfile('%s\\%s' % (t, i)), os.path.isdir('%s\\%s' % (t, i))))
  33.                 if os.path.isfile(t + '\\' + i):
  34.                     os.remove(t.encode('utf-8') + b'\\' + i.encode('utf-8'))
  35.                 elif os.path.isdir(t + '\\' + i):
  36.                     os.removedirs(
  37.                         t.encode('utf-8') + b'\\' + i.encode('utf-8'))
  38.         finally:
  39.             os.rmdir(t.encode('utf-8'))
  40.  
  41.  
  42. def WriteAnswer(pick):
  43.     f = open('pickrandom - %s.txt' % PickRandom(lst), 'wb')
  44.     try:
  45.         f.write(b'I choose this: ' + pick.encode('utf-8'))
  46.     finally:
  47.         f.close()
  48.  
  49.  
  50. def Navigate():
  51.     t = text.get().split('\n')[1]
  52.     # For some reason encod into utf-8 doesn't allow explorer to navigate:
  53.     # e = os.path.abspath(t.encode('utf-8'))
  54.     os.system(r'start explorer "%s"' % os.path.abspath(t))
  55.  
  56.  
  57. f = ttk.Frame(root).pack()
  58. Label(f, textvariable=text).pack()
  59. brickroll = ttk.Button(f, text='Roll!', underline=0)
  60. brickroll.bind('<1>', lambda e: PickRandom(lst))
  61. brickroll.bind('<space>', lambda e: PickRandom(lst), add='+')
  62. brickroll.pack()
  63. ttk.Button(f, text='Open it!', underline=2, command=Navigate).pack()
  64. ttk.Button(f, text='Delete it!', command=Delete).pack()
  65. # ttk.Button(f, text='Exit', command=root.destroy).pack()
  66. root.bind('<Escape>', lambda e: root.destroy())
  67. root.bind('<Key-r>', lambda e: PickRandom(lst), add='+')
  68. root.bind('<Key-e>', lambda e: Navigate(), add='+')
  69. root.title('PickRandom')
  70. root.minsize(200, 32)
  71. root.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement