Quantcast
Channel: Active questions tagged python - Stack Overflow
Viewing all articles
Browse latest Browse all 16891

Python how to use a value from another function and bind the function to a Tkinter button?

$
0
0

I'm new to programming in general. I have made a simple tool that converts images into WebP format.

from PIL import Imagefrom tkinter import Tk, PhotoImagefrom tkinter import filedialog as fdfrom tkinter.ttk import Label, Button# Set up window and configure the sizeswindow = Tk()window.title('Webp Converter')window.geometry('300x150')window.eval('tk::PlaceWindow . center')window.tk.call('tk', 'scaling', 1.5)icon = PhotoImage(file="uhggg-16.png")window.iconphoto(False, icon)# Define functionsdef uploadConvert():    filepath = fd.askopenfilename()    filename = filepath.split('.')[0]    image = Image.open(filepath)    image = image.convert('RGB')    if image.size[0] > 700 or image.size[1] > 700:        image.thumbnail(size=((700, 700)))    image.save(f'{filename}.webp', 'webp')# Labels and fieldslbl = Label(window, text='Select your image to convert:')lbl.grid(column=0, row=0, padx=20, pady=20)btn_upload = Button(text='Upload and convert', command=uploadConvert)btn_upload.grid(column=0, row=1)window.mainloop()

Everything works fine here.

I wanted to add a feature that display the selected image's size (width x height) in 2 input fields and you can change width and height, width/height ratio stays the same, or you can leave it as it is and proceed webp conversion.I know I will need an extra button and 2 input fields to do that. But I don't know what exactly to do.

I tried to split the uploadConvert() function into 2 functions: upload() and convert(). upload() opens the image and get the image name, convert(filename, image) receives 2 parameters from upload() and convert the image. upload() and convert() will be bind to 2 separate ttk buttons.

The first issue I encountered is that convert() won't get the objects from upload() and also I don't know how exactly I should bind a function to button with multiple params using "command=" for ttk buttons.

I have also tried to add 2 Entry as width and height display/input fields..


Viewing all articles
Browse latest Browse all 16891


<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>