John_245 0 Newbie Poster

Any WRONG WITH THIS CODE? I AM BEGINNER TO THIS .>>>

#!/use/bin/env python3
import os
import tkinter
import webbrowser
from tkinter import *
from tkinter.filedialog import asksaveasfilename
from tkinter.scrolledtext import ScrolledText
from PIL import Image, ImageTk
from PIL import *

window = tkinter.Tk()
window.geometry("520x800")
window.title("STARLABS BIOSCIENCE SDN BHD")
window.resizable(False, False)
window.config(background="#150051")

MENUBAR = Menu(window)
window.config(menu=MENUBAR)

image = Image.open("s.jpg")
photo = ImageTk.PhotoImage(image)
label = Label(window, image=photo, text="").pack()
venuelabel = Label(window, text="Venue: ", background="#150051", font=("bold", 13,)).place(x=0, y=660)
contactLabel = Label(window, text="Contact: ").place(x=0, y=720)
dateTEXT = Entry(window, width=35, ).place(x=170, y=380)
Venue = ScrolledText(window, width=35, background="#150051").place(x=57, y=660, height=60)
person = Entry(window, width=35).place(x=57, y=720)

# function :
def exitWindow():
    window.destroy()

def save():
    print("save")
    dialogue = image.filename = asksaveasfilename(initialdir="/", title="Select file", filetypes=(
        ('JPEG', ('*.jpg', '*.jpeg', '*.jpe')), ('PNG', '*.png'), ('BMP', ('*.bmp', '*.jdib')), ('GIF', '*.gif')))
    image.save("picture.jpg")

# Add Menu Items:
file_menu = Menu(MENUBAR, tearoff=0)
file_menu.add_cascade(label="Save", command=save)
addon = Menu(file_menu, tearoff=0)
addon.add_command(label="SAVE TO FILE")
addon.add_command(label="Email To")
file_menu.add_command(label="Exit", command=exitWindow)
MENUBAR.add_cascade(label="File", menu=file_menu)

window.mainloop()

Dani AI

Generated

A short, focused checklist for — the save dialog returns a path that must be used, and you must guard for cancel and for format mismatches. The Tk file-dialog functions accept options like filetypes and defaultextension and will return the selected filename (or nothing if cancelled), so check the result before trying to save. (docs.python.org)

Pillow’s Image.save() will normally pick the file format from the filename extension; if you save to JPEG you must ensure the image has no alpha channel (convert to RGB) or Pillow will raise an error (JPEG does not support transparency). Also prefer asksaveasfilename (returns a filename) over asksaveasfile (returns an open file object) when you want the path. (pillow.readthedocs.io)

A safe pattern to follow:

import os
import tkinter as tk
from tkinter.filedialog import asksaveasfilename
from tkinter import messagebox
from PIL import Image

def save_image(pil_img):
    fname = asksaveasfilename(
        title='Save image as...',
        defaultextension='.jpg',
        filetypes=[('JPEG', ('.jpg', '.jpeg')),
                   ('PNG', '.png'),
                   ('BMP', '.bmp'),
                   ('GIF', '.gif')])
    if not fname:
        return  # user cancelled

    ext = os.path.splitext(fname)[1].lower()
    fmt = {'.jpg':'JPEG', '.jpeg':'JPEG', '.png':'PNG', '.bmp':'BMP', '.gif':'GIF'}.get(ext)

    if fmt == 'JPEG' and pil_img.mode in ('RGBA', 'LA', 'P'):
        pil_img = pil_img.convert('RGB')

    try:
        pil_img.save(fname, fmt)
    except Exception as e:
        messagebox.showerror('Save failed', str(e))

Troubleshooting tips: make filetypes a sequence of (label, pattern) tuples; use defaultextension to auto-append an extension when the user omits it; and wrap save() in a try/except to surface errors (permissions, disk full, unsupported format). The Tk docs and Pillow reference are authoritative for these behaviors. (docs.python.org)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.