Advertisement
here2share

# Tk_basic_demo.py

Apr 13th, 2018
266
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 8.32 KB | None | 0 0
  1. # Tk_basic_demo.py
  2.  
  3. #!/usr/bin/env python
  4. #--------------------------
  5. #Brad
  6. #
  7. #Aug 4
  8. #
  9. #Gui testing
  10. #--------------------------
  11.  
  12. import os, sys
  13.  
  14. if sys.version_info[0] == 2:
  15.     from Tkinter import *
  16.     import tkFileDialog
  17.     import tkMessageBox
  18.     import tkSimpleDialog
  19. else:
  20.     import tkinter as Tkinter
  21.     import tkinter.filedialog as tkFileDialog
  22.     import tkinter.messagebox as tkMessageBox
  23.     import tkinter.simpledialog as tkSimpleDialog
  24.  
  25. #########################
  26. #
  27. #Global vars
  28. #
  29. #########################
  30.  
  31. labeltext = "Status"
  32.  
  33. ########################
  34. #
  35. #Simple Dialog Class (Not used)
  36. #
  37. ########################
  38. class MyDialog(tkSimpleDialog.Dialog):
  39.  
  40.     def body(self, master):
  41.  
  42.         Label(master, text="First:").grid(row=0)
  43.         Label(master, text="Second:").grid(row=1)
  44.  
  45.         self.e1 = Entry(master)
  46.         self.e2 = Entry(master)
  47.  
  48.         self.e1.grid(row=0, column=1)
  49.         self.e2.grid(row=1, column=1)
  50.         return self.e1 # initial focus
  51.  
  52.     def apply(self):
  53.         first = string.atoi(self.e1.get())
  54.         second = string.atoi(self.e2.get())
  55.         print (first)
  56.  
  57. ##########################
  58. #
  59. #Define Functions
  60. #
  61. ###########################
  62. def windowclose():
  63.     if tkMessageBox.askokcancel("Quit", "Do you really wish to quit?"):
  64.         root.destroy()     
  65.  
  66. def tempwindow2():
  67.     tkMessageBox.showwarning("Hi","close me")
  68.  
  69. def middlemouseclick(event):
  70.     if tkMessageBox.askokcancel("Quit", "Do you really wish to quit?"):
  71.         root.destroy()
  72.  
  73. #####CAPTURE MOUSE MOTION AND UPDATE LABELS############
  74. def mousemotion(event):
  75.         sx.set(event.x)
  76.         sy.set(event.y)
  77.  
  78. #####CAPTURE MOUSE CLICK AND UPDATE LABELS#############
  79. def mouseclick(event):
  80.         cx.set(event.x)
  81.         cy.set(event.y)
  82.  
  83. ########CHANGE THE BACKGROUND BASED ON SCALE VALUE######
  84. def scaleupdate(sc):
  85.         #print (scalevalue.get())
  86.         if scalevalue.get() == 0:
  87.             frame.config(bg="lightblue")
  88.         elif scalevalue.get() == 1:
  89.             frame.config(bg="blue")
  90.         elif scalevalue.get() == 3:
  91.             frame.config(bg="red")
  92.         elif scalevalue.get() ==5:
  93.             frame.config(bg="green")
  94.         else:
  95.             frame.config(bg="yellow")
  96.  
  97. ########UPDATE THE LABELS IN THE STATUS BAR########
  98. def labelupdate(newtext):
  99.         l.set(newtext)
  100.         if ckb.get():
  101.             n.set(n.get()+5)
  102.         else:
  103.             n.set(n.get()+1)
  104.  
  105. ###########INPUT DATA################
  106. def inputdata():
  107.     x = tkSimpleDialog.askinteger("Input","Multiplier")
  108.     if x:
  109.         tkMessageBox.showwarning("Yes",x)
  110.         listbox.insert(END, x)
  111.     else:
  112.         tkMessageBox.showwarning("No","You did it wrong")
  113.  
  114. ############SELECT FILE#################
  115. def fileselect():
  116.     filename = tkFileDialog.askopenfilename()
  117.     if filename:
  118.         tkMessageBox.showwarning("Yes", filename)
  119.     else:
  120.         tkMessageBox.showwarning("No", "You did it wrong")
  121.  
  122. #####################################
  123. #
  124. #MAIN
  125. #
  126. #####################################
  127.  
  128. root = Tk()
  129.  
  130. ###########ROOT WINDOW PROPERTIES#####
  131. root.resizable(False,False)
  132. root.iconbitmap(default="")  ##input your own ico file here. this will replace the default red Tk icon
  133. root.title("Test")
  134. root.attributes("-alpha", 1.0)
  135. root.protocol("WM_DELETE_WINDOW", windowclose)
  136.  
  137. ###############main frame##############
  138. frame = Frame(root,relief='raised', border=4, bg="lightblue", padx=12)
  139. frame.bind("<a>", mousemotion)
  140. frame.bind("<a>", middlemouseclick)
  141. frame.grid(column =0,row=1, sticky='w')
  142.  
  143. #############CLICK HERE#################
  144. label = Label(frame, text="Click Here", cursor="gumby")
  145. label.bind("<Enter>", mouseclick)
  146. label.grid(column=0,row=0)
  147.  
  148. ############HELLO########################
  149. message = Message(frame, text="Hello")
  150. message.grid(column=1,row=0)
  151.  
  152. ############SCALE##########################
  153. scalevalue = DoubleVar()
  154. scalevalue.set(0)
  155. scale = Scale(frame, activebackground="blue", background="red", to="10", troughcolor="green", width="25", command= scaleupdate, variable=scalevalue )
  156. scale.grid(column=0,row=1, columnspan=2)
  157.  
  158. ###################STATUS BAR###############
  159. l = StringVar()
  160. l.set("Status")
  161. status = Label(root, textvariable=l, bd=1, relief=SUNKEN, anchor='w')
  162. status.grid(column=0, row=4, sticky='w')
  163.  
  164. n = IntVar()
  165. n.set(0)
  166. status2 = Label(root, textvariable=n, bd=1, relief=SUNKEN, anchor='w')
  167. status2.grid(column=0, row=4, sticky='w', padx=40)
  168.  
  169. sx = IntVar()
  170. sx.set(0)
  171. statusx = Label(root, textvariable=sx, bd=1, relief=SUNKEN, anchor='w')
  172. statusx.grid(column=0, row=4, sticky='w', padx=65)
  173.  
  174. sy = IntVar()
  175. sy.set(0)
  176. statusy = Label(root, textvariable=sy, bd=1, relief=SUNKEN, anchor='w')
  177. statusy.grid(column=0, row=4, sticky='w', padx=95)
  178.  
  179. cx = IntVar()
  180. cx.set(0)
  181. statuscx = Label(root, textvariable=cx, bd=1, relief=SUNKEN, anchor='w')
  182. statuscx.grid(column=0, row=4, sticky='w', padx=125)
  183.  
  184. cy = IntVar()
  185. cy.set(0)
  186. statuscy = Label(root, textvariable=cy, bd=1, relief=SUNKEN, anchor='w')
  187. statuscy.grid(column=0,row=4,sticky='w', padx=140)
  188.  
  189. ####################RADIO BUTTONS###########
  190. v = IntVar()
  191. radbutton = Radiobutton(frame, text = "Option 1", variable=v, value=1)
  192. radbutton.grid(column = 2, row = 0, sticky = NW)
  193.  
  194. radbutton2 = Radiobutton(frame, text = "Option 2", variable=v, value=2)
  195. radbutton2.grid(column = 2, row = 1, sticky = NW)
  196.  
  197. #################CHECK BUTTONS##############
  198. ckb = IntVar()
  199. ckbbutton = Checkbutton(frame, text = "Add 5", variable=ckb)
  200. ckbbutton.grid(column = 2, row= 2, sticky=NW)
  201.  
  202. #################LIST BOX#####################
  203.  
  204. listbox = Listbox(frame, selectmode=MULTIPLE)
  205. listbox.grid(column=2, row=3, sticky=NW)
  206.  
  207. listbox.insert(END, "zero")
  208. listbox.insert(END, "one")
  209. listbox.insert(END, "two")
  210.  
  211. ###################IMAGES IN BASE64 ############
  212. img00 = PhotoImage(format='gif', data="R0lGODlhKwAQAJEAACWgA/3bYYNOGgAAACwAAAAAKwAQAAACfIyPqcsrD2M0oAJqa8h29yAkITiG3HWmKWiUrdtpseZdtcfmJSyjvf2Z5Q671u0wA9I+RtLjZcwgfxglTTchjqS34JUrCUMQySOzih07Tb62eeneqSfU8vsmf65xZa8S/zI3dlLD5deRl1dlxdT4MYIA2TBJuSZ2iZkZVgAAOw==")
  213. img01 = PhotoImage(format='gif', data="R0lGODlhDwAPAKECAAAAzMzM/////wAAACwAAAAADwAPAAACIISPeQHsrZ5ModrLlN48CXF8m2iQ3YmmKqVlRtW4MLwWACH+H09wdGltaXplZCBieSBVbGVhZCBTbWFydFNhdmVyIQAAOw==")
  214. cat = PhotoImage(file="") ##place your own .gif file here
  215. open = PhotoImage(format='gif', data="R0lGODlhEAAQAIcAADFKY0L/QplnAZpoApxqBJ5sBqBuCKJwCqNxC6RyDKVzDad1D6x6FLB+GLOBG7WCHbeEH7qHIr2KJcaaGcaaGsKPKsiVMMmWMcuYM8yZNMmgIc+iJte4QNq/bOKzQ+LBUP3VcP/bdfDkev/kf5SlvZylvbe3t5ytxqW11qm92r3GxrnK5P/XhP/rhP/viffwif/4k///mf//nP//pcTExMXFxc3NzdHR0cbW69jh8efv9+vz//r7/P///wAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAACH5BAMAAAEALAAAAAAQABAAAAiZAAMIHEiwoMGDBzNkwHDBAkKBGXpI5MGjAsKIMjJm7CEhAoQHDhoIxNBDo0mJEhncCHChB4yXMGPKWFBjgs2bOG1+aIGAxoQYJk3G6DCBhQGfQGPClPFiAogCNAL8dEG1KtUZGjwQiPpTxoivYEfM4LBhQFSpMUKoXatWBAUBNQROUECXboIDBgoQGGDCxkAbNAILHuz34cGAADs=")
  216.  
  217. imglabel = Label(frame, image=cat)
  218. imglabel.grid(column=3, row=0, rowspan=4)
  219.  
  220. ############TOOLBAR#####################
  221. toolbar = Frame(root)
  222. b = Button(toolbar, image=open, width=20, command=tempwindow2)
  223. b.grid(column=0, row=0, sticky='w')
  224. b2 = Button(toolbar, image=img00, width=50, command=lambda: labelupdate("status"))
  225. b2.grid(column=1, row=0)
  226. toolbar.grid(column=0, row=0, sticky='w')
  227.  
  228. #######################MENU#################
  229. menu = Menu(root)
  230. root.config(menu=menu)
  231.  
  232. filemenu = Menu(menu)
  233. menu.add_cascade(label="File", menu=filemenu)
  234. filemenu.add_command(label="Open", command=fileselect)
  235. filemenu.add_command(label="Quit", command=windowclose)
  236.  
  237. editmenu = Menu(menu)
  238. menu.add_cascade(label="Edit", menu=editmenu)
  239. editmenu.add_command(label="Input", command=inputdata)
  240.  
  241. helpmenu = Menu(menu)
  242. menu.add_cascade(label="Help", menu=helpmenu)
  243. helpmenu.add_command(label = "About", command=tempwindow2)
  244. #######ROOT MAIN###########################
  245. root.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement