PYTHON PROJECTS:
![]() |
Python game Project and GUI Application |
DOWNLOAD SOURCE CODE CLICK HERE
(1). PYTHON GAME PROJECT( "WATER, GUN, OR SNAKE" ):
DESCRIPTION:
The 'import random' statement imports the 'random' module, which 'is used to generate random numbers.
The 'game win' function takes two arguments: 'computer' (representing the computer's choice) and you (representing your choice). It determines the winner based on the rules you provided for "water," "gun," and "snake."
The code generates a random number between 1 and 3 to determine the computer's choice. The computer's choice is assigned to the computer variable based on the random number.
The user is prompted to input their choice ("water," "gun," or "snake") using the input function, and the choice is stored in the you variable.
The game win function is called with the computer's choice and the user's choice to determine the game result.
The computer's and user's choices are displayed using the print function.
The game result (tie, the user wins, or the computer wins) is determined based on the return value of the game win function and is displayed using the print function.
SOURCE CODE:
DOWNLOAD SOURCE CODE CLICK HERE
OUTPUT:
DESCRIPTION:
- Importing
Libraries:
- Import
the required libraries tkinter, file dialog from tkinter, and Image, ImageTk
from PIL for GUI and image handling.
- Creating
the Application Class:
- Define
a class named ImageChangerApp to manage the application.
- Initializing
the GUI:
- Set
up the main window of the application and assign a title.
- Variables
and Widgets:
- Define
variables to store the current image path and user's choice.
- Create
widgets including a label for displaying images, a button to choose
images, a label for instructions, an option menu for image choices, and a
button to change images.
- Defining
Methods:
- choose_image(self):
Open a file dialog to select an image, display it on the label widget.
- change_image(self):
Change the displayed image based on the user's selected option.
- Main
Application Loop:
§ In the if __name__ ==
"__main__": block:
§ Create the main tkinter window (root).
§ Initialize the ImageChangerApp
instance with the main window.
§ Start the GUI event loop using root.mainloop().
SOURCE CODE:
from tkinter import filedialog
from PIL import Image, ImageTk
class ImageChangerApp:
def __init__(self, root):
self.root = root
self.root.title("Gaurav 1st executable software")
self.current_image_path = None
self.current_choice = None
self.image_label = tk.Label(root)
self.image_label.pack()
self.choose_button = tk.Button(root, text="CHOOSE A IMAGE FROM YOUR COMPUTER TO SHOW ON SOFTWARE", command=self.choose_image)
self.choose_button.pack()
self.choice_label = tk.Label(root, text="CHOOSE AN OPTION GIVEN BELOW :")
self.choice_label.pack()
self.choice_var = tk.StringVar()
self.choice_var.set("CHOOSE IMAGE ")
self.choice_menu = tk.OptionMenu(root, self.choice_var, "zeeshan image", "ashirwad image", "gaurav image")
self.choice_menu.pack()
self.change_button = tk.Button(root, text="Change Image", command=self.change_image)
self.change_button.pack()
def choose_image(self):
self.current_image_path = filedialog.askopenfilename(filetypes=[("Image Files", "*.png *.jpg *.jpeg")])
if self.current_image_path:
image = Image.open(self.current_image_path)
image = image.resize((300, 300)) # Resize the image to fit the label
self.current_image = ImageTk.PhotoImage(image)
self.image_label.config(image=self.current_image)
def change_image(self):
self.current_choice = self.choice_var.get()
if self.current_choice == "zeeshan image":
new_image_path = r"C:\Users\madde\Desktop\python project\zeeshan.jpg"
elif self.current_choice == "ashirwad image":
new_image_path = r"C:\Users\madde\Desktop\python project\ashirwad.jpg"
elif self.current_choice == "gaurav image":
new_image_path = r"C:\Users\madde\Desktop\python project\zeeshan.jpg"
else:
new_image_path = None
if new_image_path:
image = Image.open(new_image_path)
image = image.resize((300, 300)) # Resize the image to fit the label
new_image = ImageTk.PhotoImage(image)
self.image_label.config(image=new_image)
self.image_label.image = new_image
if __name__ == "__main__":
root = tk.Tk()
app = ImageChangerApp(root)
root.mainloop()
0 Comments