Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python3
- # Author: Jen [jen.development@gmail.com]
- # Creates a circular graph with the values given.
- ## Improve the CenterWindow() function so it takes into account the outermost
- ## border. *Not accurately centered now.
- from tkinter import *
- from tkinter import messagebox
- def CenterWindow(win, width, height):
- win.update_idletasks()
- x=(win.winfo_screenwidth() // 2) - (width // 2)
- y=(win.winfo_screenheight() // 2) - (height // 2)
- return [width, height, x, y]
- def MakeGraph():
- try:
- Value=ValueObj.get()
- Total=TotalObj.get()
- except ValueError:
- InputAlert=messagebox.showwarning(title="Wrong Input", message="Enter valid numbers.")
- return
- if Total > Value:
- ValueDeg=360*(Value/Total)
- Arc=Graph.create_arc(25, 15, 305, 300, start=0, extent=ValueDeg, fill="red")
- Rest=Graph.create_arc(25, 15, 305, 300, start=ValueDeg, extent=(360-ValueDeg), fill="#DADADA")
- return
- else:
- NumbersAlert=messagebox.showwarning(title="Wrong Values", message="Total number must be greater than Value number.")
- return
- mGui=Tk()
- ValueObj=IntVar()
- TotalObj=IntVar()
- WinWidth=700
- WinHeight=400
- mGui.withdraw() # Hides the window from the screen
- mGui.geometry("{0[0]}x{0[1]}+{0[2]}+{0[3]}".format(CenterWindow(mGui, WinWidth, WinHeight)))
- mGui.deiconify() # Displays the window in the screen again
- mGui.title("Circular Graphic")
- # Canvas
- Graph=Canvas(mGui, width=330, height=315, bg="white")
- Graph.place(x=330, y=42)
- # Labels
- InfoLabel=Label(mGui, text="Information:", font="default 10 bold").place(x=70, y=80)
- ValueLabel=Label(mGui, text="Value:").place(x=73, y=120)
- TotalLabel=Label(mGui, text="Total:").place(x=77, y=150)
- VaUnit=Label(mGui, text="u").place(x=265, y=122)
- ToUnit=Label(mGui, text="u").place(x=265, y=152)
- NoteLabel=Label(mGui, text="\"u\" stands for units.", font="default 8 italic").place(x=73, y=227)
- # Entries
- ValueEntry=Entry(mGui, width=18, textvariable=ValueObj).place(x=115, y=120)
- TotalEntry=Entry(mGui, width=18, textvariable=TotalObj).place(x=115, y=150)
- # Button
- DoneButton=Button(mGui, text="Done", command=MakeGraph).place(x=160, y=180)
- mGui.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement