|
| 1 | +# Python Program to implement Astrologer's Stars logic in GUI. |
| 2 | + |
| 3 | + |
| 4 | +# Imports |
| 5 | +import os |
| 6 | +import sys |
| 7 | +from PIL import Image |
| 8 | +from tkinter.tix import Tk, Balloon |
| 9 | +from tkinter import END, RIDGE, Button, Entry, Frame, Label, Text, Toplevel, messagebox |
| 10 | + |
| 11 | +# creating a path for the icon of the window. |
| 12 | +def resource_path(relative_path): |
| 13 | + """Get absolute path to resource, works for dev and for PyInstaller.""" |
| 14 | + |
| 15 | + base_path = getattr(sys, '_MEIPASS', os.path.dirname(os.path.abspath(__file__))) |
| 16 | + return os.path.join(base_path, relative_path) |
| 17 | + |
| 18 | +path = resource_path("./Icon//Icon.ico") |
| 19 | + |
| 20 | + |
| 21 | +def show(a): |
| 22 | + """Function to show the stars.""" |
| 23 | + |
| 24 | + type(a) # Garbage calculation to make use of the unused argument. |
| 25 | + |
| 26 | + try: # Try block to check if the input of n. |
| 27 | + int(en1.get()) |
| 28 | + except Exception: # If the input is invalid, show an error message. |
| 29 | + if len(en1.get()) == 0: |
| 30 | + messagebox.showerror("Error", "Please enter a number.") |
| 31 | + else: |
| 32 | + messagebox.showerror("Error", "Please enter a valid number.") |
| 33 | + |
| 34 | + try: # Try block to check if the input of b. |
| 35 | + int(en2.get()) |
| 36 | + except Exception: # If the input is invalid, show an error message. |
| 37 | + if len(en2.get()) == 0: |
| 38 | + messagebox.showerror("Error", "Please enter a number.") |
| 39 | + else: |
| 40 | + messagebox.showerror("Error", "Please enter a valid number.") |
| 41 | + |
| 42 | + if int(en1.get()) <= 0 or int(en2.get()) < 0: # If the input is invalid, show an error message. |
| 43 | + messagebox.showerror("Error", "Please enter a positive number.") |
| 44 | + |
| 45 | + else: # If the input is valid, show the stars. |
| 46 | + |
| 47 | + try: # Try block. |
| 48 | + n = int(en1.get()) # Taking the input of n. |
| 49 | + b = int(en2.get()) # Taking the input of b. |
| 50 | + b = bool(b) # Converting the integer b to bool. |
| 51 | + |
| 52 | + except Exception as e: # If the input is invalid, show an error message. |
| 53 | + n = b = 0 |
| 54 | + messagebox.showerror("Error", str(e)) |
| 55 | + |
| 56 | + top1 = Toplevel(root1) # Creating a new window. |
| 57 | + |
| 58 | + # Setting the title of the window. |
| 59 | + top1.title("Stars Output") |
| 60 | + |
| 61 | + # Setting the icon of the window. |
| 62 | + try: # Try block to set the icon of the window. |
| 63 | + top1.iconbitmap(path) |
| 64 | + except Exception: # If the icon is not found, show an error message. |
| 65 | + messagebox.showerror("Error", "icon.ico not found.") |
| 66 | + |
| 67 | + # Setting the size of the window. |
| 68 | + top1.geometry("500x500") |
| 69 | + top1.minsize(200, 200) |
| 70 | + top1.maxsize(850, 700) |
| 71 | + |
| 72 | + # Creating a Text. |
| 73 | + txt1 = Text(top1, relief=RIDGE, borderwidth=5, width=100, height=100) |
| 74 | + txt1.pack(padx=20, pady=10) |
| 75 | + |
| 76 | + # Creating a list containing numbers from 1 to n. |
| 77 | + v = list(range(1, n + 1)) |
| 78 | + |
| 79 | + if not b: # If the bool value turns False, the list is reversed. |
| 80 | + v.reverse() |
| 81 | + |
| 82 | + for i in v: # A loop to print the required pattern. |
| 83 | + txt1.insert(END, "*" * i + "\n") # Inserting the stars in the Text. |
| 84 | + |
| 85 | + |
| 86 | +if __name__ == '__main__': |
| 87 | + """The Main Function Begins.""" |
| 88 | + |
| 89 | + root1 = Tk() # Creating main window root1. |
| 90 | + |
| 91 | + root1.title("Astrologer's Stars") # Setting the title of root1. |
| 92 | + |
| 93 | + # Trying to Add Icon to window. |
| 94 | + try: |
| 95 | + root1.iconbitmap(path) |
| 96 | + |
| 97 | + except Exception: |
| 98 | + messagebox.showerror("Error", "Icon not found.") |
| 99 | + |
| 100 | + # Geometry of root1. |
| 101 | + |
| 102 | + root1.geometry('500x300') |
| 103 | + root1.minsize(400, 260) |
| 104 | + root1.maxsize(600, 450) |
| 105 | + |
| 106 | + # Background Color of root 1. |
| 107 | + |
| 108 | + root1['bg'] = 'grey' |
| 109 | + |
| 110 | + # Heading |
| 111 | + |
| 112 | + Heading = Label(root1, text="Astrologer's Stars", bd=5, font='Aerial 22 underline', bg='black', fg='white') |
| 113 | + Heading.pack(pady=10, ipadx=10) |
| 114 | + |
| 115 | + # Creating a frame. |
| 116 | + |
| 117 | + f1 = Frame(root1, bd=20) |
| 118 | + f1.pack() |
| 119 | + |
| 120 | + # Content. |
| 121 | + |
| 122 | + lb1 = Label(f1, text="Enter the value of n :", font="Arial 12") # Creating a Label. |
| 123 | + lb1.grid(row=0, column=0, pady=10, ipadx=3) |
| 124 | + |
| 125 | + tip1 = Balloon() # Creating a Balloon. |
| 126 | + tip1['bg'] = 'white' |
| 127 | + |
| 128 | + en1 = Entry(f1, font="Areal 12") # Creating an Entry. |
| 129 | + en1.grid(row=0, column=1, pady=10) |
| 130 | + |
| 131 | + tip1.bind_widget(en1, balloonmsg="Enter the value of n(Integer)") # Binding the Balloon to the Entry. |
| 132 | + |
| 133 | + # Repeat the same for the other Entry. |
| 134 | + lb2 = Label(f1, text="Enter the bool (0 or 1) :", font="Areal 12") |
| 135 | + lb2.grid(row=1, column=0, pady=10) |
| 136 | + |
| 137 | + tip2 = Balloon() |
| 138 | + tip2['bg'] = 'white' |
| 139 | + |
| 140 | + en2 = Entry(f1, font="Areal 12") |
| 141 | + en2.grid(row=1, column=1, pady=10) |
| 142 | + en2.bind("<Return>", show) # Binding the Entry to the show function. |
| 143 | + |
| 144 | + tip2.bind_widget(en2, balloonmsg=" Enter the value of bool(1 or 0)") |
| 145 | + |
| 146 | + tip3 = Balloon() |
| 147 | + tip3['bg'] = 'white' |
| 148 | + |
| 149 | + bt1 = Button(f1, text="Show", cursor='hand2', font="Arial 12") # Creating a Button. |
| 150 | + bt1.grid(row=2, columnspan=2, pady=10, ipadx=7) |
| 151 | + |
| 152 | + bt1.bind("<Button-1>", show) # Binding the Button to the show function. |
| 153 | + |
| 154 | + tip3.bind_widget(bt1, balloonmsg="Click to view the Pattern.") # Binding the Balloon to the Button. |
| 155 | + |
| 156 | + root1.mainloop() # Mainloop of root1. |
0 commit comments