Advertisement
Guest User

[Python3] Gráfico circular con Tkinter.

a guest
Jul 7th, 2014
186
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.22 KB | None | 0 0
  1. #!/usr/bin/env python3
  2.  
  3. # Author: Jen [jen.development@gmail.com]
  4. # Creates a circular graph with the values given.
  5.  
  6. ## Improve the CenterWindow() function so it takes into account the outermost
  7. ## border. *Not accurately centered now.
  8.  
  9. from tkinter import *
  10. from tkinter import messagebox
  11.  
  12. def CenterWindow(win, width, height):
  13.     win.update_idletasks()
  14.     x=(win.winfo_screenwidth() // 2) - (width // 2)
  15.     y=(win.winfo_screenheight() // 2) - (height // 2)
  16.     return [width, height, x, y]    
  17.  
  18. def MakeGraph():
  19.     try:
  20.         Value=ValueObj.get()
  21.         Total=TotalObj.get()
  22.     except ValueError:
  23.         InputAlert=messagebox.showwarning(title="Wrong Input", message="Enter valid numbers.")
  24.         return
  25.  
  26.     if Total > Value:
  27.         ValueDeg=360*(Value/Total)
  28.         Arc=Graph.create_arc(25, 15, 305, 300, start=0, extent=ValueDeg, fill="red")
  29.         Rest=Graph.create_arc(25, 15, 305, 300, start=ValueDeg, extent=(360-ValueDeg), fill="#DADADA")
  30.         return
  31.     else:
  32.         NumbersAlert=messagebox.showwarning(title="Wrong Values", message="Total number must be greater than Value number.")
  33.         return
  34.  
  35. mGui=Tk()
  36.  
  37. ValueObj=IntVar()
  38. TotalObj=IntVar()
  39. WinWidth=700
  40. WinHeight=400
  41.  
  42. mGui.withdraw() # Hides the window from the screen
  43. mGui.geometry("{0[0]}x{0[1]}+{0[2]}+{0[3]}".format(CenterWindow(mGui, WinWidth, WinHeight)))
  44. mGui.deiconify() # Displays the window in the screen again
  45. mGui.title("Circular Graphic")
  46.  
  47. # Canvas
  48. Graph=Canvas(mGui, width=330, height=315, bg="white")
  49. Graph.place(x=330, y=42)
  50.  
  51. # Labels
  52. InfoLabel=Label(mGui, text="Information:", font="default 10 bold").place(x=70, y=80)
  53.  
  54. ValueLabel=Label(mGui, text="Value:").place(x=73, y=120)
  55. TotalLabel=Label(mGui, text="Total:").place(x=77, y=150)
  56.  
  57. VaUnit=Label(mGui, text="u").place(x=265, y=122)
  58. ToUnit=Label(mGui, text="u").place(x=265, y=152)
  59.  
  60. NoteLabel=Label(mGui, text="\"u\" stands for units.", font="default 8 italic").place(x=73, y=227)
  61.  
  62. # Entries
  63. ValueEntry=Entry(mGui, width=18, textvariable=ValueObj).place(x=115, y=120)
  64. TotalEntry=Entry(mGui, width=18, textvariable=TotalObj).place(x=115, y=150)
  65.  
  66. # Button
  67. DoneButton=Button(mGui, text="Done", command=MakeGraph).place(x=160, y=180)
  68.  
  69.  
  70. mGui.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement