Hi There looking for some help - im trying to create a 6 ball lottery generator for my local community sports club - i have no real python experience & have been using ChatGPT to do the coding for me. i have the bare bones working but when trying to fine tune the AI keep deleting or tweaking sections that were previously working OK
what i want to achieve is the following
3 main buttons (select Background image | Generate Numbers | Save Image)
i want the background image to remain saved in the prgram until the next time the background image has been selected. - i have this working
i want a welcome label “welcome to GRFC Lottery”
i want another label with “this weeks numbers are”
i would like 6 different coloured ‘balls’ overlaid ontop of the background image
inside each ‘ball’ is to have a randomly generated number
i’d like to have a settings tab that has a set of sliders that control the position / size of the text labels to allow for adjustment
I like the text label to centrally aligned to the background image
I would like the coloured balls to be a little more than a rough pixelated circle
i would also like to have the program export the numbers to a text file or excel file each time the generate numbers button has been clicked & record date & time (to allow for security & quality control).
any help much appreciated (below is the code i have so far)
Steven
import tkinter as tk
import random
import math
from tkinter import filedialog
from PIL import Image, ImageDraw, ImageTk
import json
# Create the main window
root = tk.Tk()
root.title("Colored Circles with Random Numbers")
# Increase the canvas size
canvas_width = 1000 # Increased width
canvas_height = 600 # Increased height
# Create a canvas to display the background image and circles
canvas = tk.Canvas(root, width=canvas_width, height=canvas_height)
canvas.pack()
# Function to handle selecting a background image
def select_background_image():
file_path = filedialog.askopenfilename(filetypes=[("Image files", "*.jpg *.png *.gif")])
if file_path:
# Display the selected image as the background
background_image = Image.open(file_path)
# Calculate the scaling factors to maintain the aspect ratio
width_factor = canvas_width / background_image.width
height_factor = canvas_height / background_image.height
min_factor = min(width_factor, height_factor)
new_width = int(background_image.width * min_factor)
new_height = int(background_image.height * min_factor)
background_image = background_image.resize((new_width, new_height), Image.BICUBIC)
background_photo = ImageTk.PhotoImage(background_image)
canvas.create_image(0, 0, image=background_photo, anchor=tk.NW)
canvas.background = background_photo # Store a reference to the PhotoImage object
# Save the path of the selected image in a JSON configuration file
config = {"background_image_path": file_path}
with open("config.json", "w") as config_file:
json.dump(config, config_file)
else:
print("No image selected or invalid file path") # Debug message
# Create a "Select Background Image" button
select_background_button = tk.Button(root, text="Select Background Image", command=select_background_image)
select_background_button.pack(side=tk.LEFT, padx=10) # Adjust padding and position
# Function to save the displayed image as a JPEG file
def save_image_as_jpeg():
try:
# Create an empty white image
image = Image.new("RGB", (canvas_width, canvas_height), "white")
# Create a drawing context
draw = ImageDraw.Draw(image)
# Rest of your code for drawing circles
# Prompt the user to choose a file location and filename to save the image as a JPG file
save_file_path = filedialog.asksaveasfilename(defaultextension=".jpg", filetypes=[("JPEG files", "*.jpg")])
# Save the image as a JPG file if a save file is provided
if save_file_path:
image.save(save_file_path, "JPEG")
except Exception as e:
print("An error occurred while saving the image:", str(e)) # Debug message
# Create a "Save Image" button
save_button = tk.Button(root, text="Save Image", command=save_image_as_jpeg)
save_button.pack(side=tk.LEFT, padx=10) # Adjust padding and position
# Function to generate random lottery numbers and update the label text
def generate_lottery_numbers():
numbers = random.sample(range(1, 50), 6)
numbers_text = "Your lucky numbers are: " + " ".join(map(str, numbers))
# Create a label to display the generated lottery numbers with a colored text and a transparent background
numbers_label = tk.Label(root, text=numbers_text, font=("Helvetica", 20), fg="blue", bg="white")
canvas.create_window(canvas_width/2, canvas_height/2, window=numbers_label)
# Create a "Generate Numbers" button
generate_button = tk.Button(root, text="Generate Numbers", command=generate_lottery_numbers)
generate_button.pack(side=tk.LEFT, padx=10) # Adjust padding and position
# Create a label for "Glenrothes Rugby Club Lottery" and move it down in the Y direction
lottery_label = tk.Label(root, text="Glenrothes Rugby Club Lottery", font=("Helvetica", 24), fg="dark grey")
lottery_label.place(x=20, y=400) # Adjust the Y coordinate to move it down
# Check if there is a previously selected background image in the configuration file
try:
with open("config.json", "r") as config_file:
config = json.load(config_file)
if "background_image_path" in config:
# Display the previously selected image as the background
background_image = Image.open(config["background_image_path"])
# Calculate the scaling factors to maintain the aspect ratio
width_factor = canvas_width / background_image.width
height_factor = canvas_height / background_image.height
min_factor = min(width_factor, height_factor)
new_width = int(background_image.width * min_factor)
new_height = int(background_image.height * min_factor)
background_image = background_image.resize((new_width, new_height), Image.BICUBIC)
background_photo = ImageTk.PhotoImage(background_image)
canvas.create_image(0, 0, image=background_photo, anchor=tk.NW)
canvas.background = background_photo # Store a reference to the PhotoImage object
except FileNotFoundError:
print