ΠΠ°ΠΊ Π²ΡΡΠ°Π²ΠΈΡΡ ΠΊΠ°ΡΡΠΈΠ½ΠΊΡ Π² ΡΠΊΠΈΠ½ΡΠ΅Ρ
ΠΠ°ΠΊ Π΄ΠΎΠ±Π°Π²ΠΈΡΡ ΠΈΠ·ΠΎΠ±ΡΠ°ΠΆΠ΅Π½ΠΈΠ΅ Π² Tkinter?
ΠΠ°ΠΊ Π΄ΠΎΠ±Π°Π²ΠΈΡΡ ΠΈΠ·ΠΎΠ±ΡΠ°ΠΆΠ΅Π½ΠΈΠ΅ Π² Tkinter?
ΠΡΠΎ Π΄Π°Π»ΠΎ ΠΌΠ½Π΅ ΡΠΈΠ½ΡΠ°ΠΊΡΠΈΡΠ΅ΡΠΊΡΡ ΠΎΡΠΈΠ±ΠΊΡ:
9 ΠΎΡΠ²Π΅ΡΠΎΠ²
ΠΡΡΡ Π»ΠΈ ΠΊΠ°ΠΊΠΎΠΉ-Π½ΠΈΠ±ΡΠ΄Ρ ΡΠΏΠΎΡΠΎΠ± Π΄ΠΎΠ±Π°Π²ΠΈΡΡ ΠΏΠΎΠ»ΡΠ·ΠΎΠ²Π°ΡΠ΅Π»ΡΡΠΊΠΎΠ΅ ΠΈΠ·ΠΎΠ±ΡΠ°ΠΆΠ΅Π½ΠΈΠ΅ ΠΊ tkinter, Π²ΡΡΡΠΎΠ΅Π½Π½ΠΎΠΌΡ Π² tkMessageBox?
Python 3.3.1 [MSC v.1600 32 Π±ΠΈΡ (Intel)] Π½Π° win32 14.May.2013
ΠΡΠΎ ΡΡΠ°Π±ΠΎΡΠ°Π»ΠΎ Π΄Π»Ρ ΠΌΠ΅Π½Ρ, ΡΠ»Π΅Π΄ΡΡ ΠΏΡΠΈΠ²Π΅Π΄Π΅Π½Π½ΠΎΠΌΡ Π²ΡΡΠ΅ ΠΊΠΎΠ΄Ρ
ΠΡΠΈΠ²Π΅Π΄Π΅Π½Π½ΡΠΉ Π²ΡΡΠ΅ ΠΏΡΠΈΠΌΠ΅Ρ ΠΎΡΠ»ΠΈΡΠ½ΠΎ ΡΡΠ°Π±ΠΎΡΠ°Π» Π΄Π»Ρ ΠΌΠ΅Π½Ρ, ΡΠ΅ΡΡΠΈΡΡΡ Π½Π° ΠΈΠ½ΡΠ΅ΡΠ°ΠΊΡΠΈΠ²Π½ΠΎΠΌ ΠΈΠ½ΡΠ΅ΡΠΏΡΠ΅ΡΠ°ΡΠΎΡΠ΅.
ΠΠΎΡ ΠΏΡΠΈΠΌΠ΅Ρ Π΄Π»Ρ Python 3, ΠΊΠΎΡΠΎΡΡΠΉ Π²Ρ ΠΌΠΎΠΆΠ΅ΡΠ΅ ΠΎΡΡΠ΅Π΄Π°ΠΊΡΠΈΡΠΎΠ²Π°ΡΡ Π΄Π»Ρ Python 2 π
Π‘Π»Π΅Π΄ΡΡΡΠΈΠΉ ΠΊΠΎΠ΄ ΡΠ°Π±ΠΎΡΠ°Π΅Ρ Π½Π° ΠΌΠΎΠ΅ΠΉ ΠΌΠ°ΡΠΈΠ½Π΅
ΡΠ±Π΅Π΄ΠΈΡΠ΅ΡΡ, ΡΡΠΎ Ρ Π²Π°Ρ ΡΡΡΠ°Π½ΠΎΠ²Π»Π΅Π½ ΠΏΠ°ΠΊΠ΅Ρ PIL
ΠΡΠΎ Π½Π΅ ΡΡΠ°Π½Π΄Π°ΡΡΠ½Π°Ρ Π±ΠΈΠ±Π»ΠΈΠΎΡΠ΅ΠΊΠ° python 2.7. ΠΠΎΡΡΠΎΠΌΡ, ΡΡΠΎΠ±Ρ ΠΎΠ½ΠΈ ΡΠ°Π±ΠΎΡΠ°Π»ΠΈ Π΄ΠΎΠ»ΠΆΠ½ΡΠΌ ΠΎΠ±ΡΠ°Π·ΠΎΠΌ, ΠΈ Π΅ΡΠ»ΠΈ Π²Ρ ΠΈΡΠΏΠΎΠ»ΡΠ·ΡΠ΅ΡΠ΅ Python 2.7, Π²Ρ Π΄ΠΎΠ»ΠΆΠ½Ρ ΡΠ½Π°ΡΠ°Π»Π° Π·Π°Π³ΡΡΠ·ΠΈΡΡ Π±ΠΈΠ±Π»ΠΈΠΎΡΠ΅ΠΊΡ PIL: ΠΡΡΠΌΠ°Ρ ΡΡΡΠ»ΠΊΠ° Π΄Π»Ρ Π·Π°Π³ΡΡΠ·ΠΊΠΈ: http://effbot.org/downloads/PIL-1.1.7.win32-py2.7.exe ΠΠΎΡΠ»Π΅ Π΅Π΅ ΡΡΡΠ°Π½ΠΎΠ²ΠΊΠΈ Π²ΡΠΏΠΎΠ»Π½ΠΈΡΠ΅ ΡΠ»Π΅Π΄ΡΡΡΠΈΠ΅ Π΄Π΅ΠΉΡΡΠ²ΠΈΡ:
ΠΡΡΠ΅Π΄Π°ΠΊΡΠΈΡΡΠΉΡΠ΅ ΡΠ²ΠΎΠΉ script.py
ΠΠ°Π΄Π΅ΡΡΡ, ΡΡΠΎ ΠΏΠΎΠΌΠΎΠΆΠ΅Ρ!
ΠΠ°ΠΊ Π΄ΠΎΠ±Π°Π²ΠΈΡΡ ΠΈΠ·ΠΎΠ±ΡΠ°ΠΆΠ΅Π½ΠΈΠ΅ Π² Π²ΠΈΠ΄ΠΆΠ΅Ρ Π² tkinter? ΠΠΎΡΠ΅ΠΌΡ ΠΊΠΎΠ³Π΄Π° Ρ ΠΈΡΠΏΠΎΠ»ΡΠ·ΡΡ ΡΡΠΎΡ ΠΊΠΎΠ΄ ΠΎΠ½ Π½Π΅ ΡΠ°Π±ΠΎΡΠ°Π΅Ρ: some_widget.config(image=PhotoImage(file=test.png), compound=RIGHT) Π½ΠΎ ΡΡΠΎ ΡΠ°Π±ΠΎΡΠ°Π΅Ρ?: an_image=PhotoImage(file=test.png) some_widget.config(image=anImage, compound=RIGHT)
ΠΠΈΠΆΠ΅ ΠΏΡΠΈΠ²Π΅Π΄Π΅Π½ ΠΏΡΠΈΠΌΠ΅Ρ ΠΎΡΠΎΠ±ΡΠ°ΠΆΠ΅Π½ΠΈΡ:
ΠΈΠ»ΠΈ Π΅ΡΠ»ΠΈ tk.TkVersion :
ΠΡΠΎ ΠΏΡΠΎΠ±Π»Π΅ΠΌΠ° Ρ Π²Π΅ΡΡΠΈΠ΅ΠΉ Python. ΠΡΠ»ΠΈ Π²Ρ ΠΈΡΠΏΠΎΠ»ΡΠ·ΡΠ΅ΡΠ΅ ΠΏΠΎΡΠ»Π΅Π΄Π½ΡΡ Π²Π΅ΡΡΠΈΡ, ΡΠΎ Π²Π°Ρ ΡΡΠ°ΡΡΠΉ ΡΠΈΠ½ΡΠ°ΠΊΡΠΈΡ Π½Π΅ Π±ΡΠ΄Π΅Ρ ΡΠ°Π±ΠΎΡΠ°ΡΡ ΠΈ Π²ΡΠ΄Π°ΡΡ Π²Π°ΠΌ ΡΡΡ ΠΎΡΠΈΠ±ΠΊΡ. ΠΠΎΠΆΠ°Π»ΡΠΉΡΡΠ°, ΡΠ»Π΅Π΄ΡΠΉΡΠ΅ ΠΊΠΎΠ΄Ρ @Josav09’s, ΠΈ Π²ΡΠ΅ Π±ΡΠ΄Π΅Ρ Π² ΠΏΠΎΡΡΠ΄ΠΊΠ΅.
ΠΡΠΎΡΡΠΎ ΠΏΡΠ΅ΠΎΠ±ΡΠ°Π·ΡΠΉΡΠ΅ ΠΈΠ·ΠΎΠ±ΡΠ°ΠΆΠ΅Π½ΠΈΠ΅ Π² ΡΠΎΡΠΌΠ°ΡΠ΅ jpg Π² ΡΠΎΡΠΌΠ°Ρ png. ΠΡΠΎ Π±ΡΠ΄Π΅Ρ ΡΠ°Π±ΠΎΡΠ°ΡΡ 100%.
ΠΠΎΡ ΠΎΠΆΠΈΠ΅ Π²ΠΎΠΏΡΠΎΡΡ:
ΠΡΠΏΠΎΠ»ΡΠ·ΡΡ tkinter, Ρ ΠΏΡΡΠ°ΡΡΡ ΠΎΡΠΎΠ±ΡΠ°Π·ΠΈΡΡ ΠΈΠ·ΠΎΠ±ΡΠ°ΠΆΠ΅Π½ΠΈΠ΅ Π²Π½ΡΡΡΠΈ Π³ΡΠ°Π½ΠΈΡΡ Π²ΠΈΠ΄ΠΆΠ΅ΡΠ° Π²Π²ΠΎΠ΄Π°. Π― ΠΏΠΎΠΏΡΡΠ°Π»ΡΡ ΠΏΠΎΠΈΡΠΊΠ°ΡΡ Π² Google, Π½ΠΎ Π±Π΅Π·ΡΡΠΏΠ΅ΡΠ½ΠΎ, Ρ ΠΊΠΎΠ³ΠΎ-ΡΠΎ Π΅ΡΡΡ ΠΈΠ΄Π΅Ρ, ΠΊΠ°ΠΊ ΡΡΠΎ ΡΠ΄Π΅Π»Π°ΡΡ?
Π£ ΠΌΠ΅Π½Ρ Π΅ΡΡΡ ΠΎΡΠ½ΠΎΡΠΈΡΠ΅Π»ΡΠ½ΠΎ ΠΏΡΠΎΡΡΠΎΠΉ ΡΠΊΡΠΈΠΏΡ python, ΠΈΡΠΏΠΎΠ»ΡΠ·ΡΡΡΠΈΠΉ tkinter GUI ΠΊΠΎΠ΄ Π²ΡΠ³Π»ΡΠ΄ΠΈΡ ΠΏΡΠΈΠΌΠ΅ΡΠ½ΠΎ ΡΠ°ΠΊ from Tkinter import * master = Tk() def f1(): print function f1 does stuff, nice.. title =.
ΠΡΡΡ Π»ΠΈ ΠΊΠ°ΠΊΠΎΠΉ-Π½ΠΈΠ±ΡΠ΄Ρ ΡΠΏΠΎΡΠΎΠ± Π΄ΠΎΠ±Π°Π²ΠΈΡΡ ΠΏΠΎΠ»ΡΠ·ΠΎΠ²Π°ΡΠ΅Π»ΡΡΠΊΠΎΠ΅ ΠΈΠ·ΠΎΠ±ΡΠ°ΠΆΠ΅Π½ΠΈΠ΅ ΠΊ tkinter, Π²ΡΡΡΠΎΠ΅Π½Π½ΠΎΠΌΡ Π² tkMessageBox?
ΠΠ°ΠΊ Π΄ΠΎΠ±Π°Π²ΠΈΡΡ ΠΈΠ·ΠΎΠ±ΡΠ°ΠΆΠ΅Π½ΠΈΠ΅ Π² Π²ΠΈΠ΄ΠΆΠ΅Ρ Π² tkinter? ΠΠΎΡΠ΅ΠΌΡ ΠΊΠΎΠ³Π΄Π° Ρ ΠΈΡΠΏΠΎΠ»ΡΠ·ΡΡ ΡΡΠΎΡ ΠΊΠΎΠ΄ ΠΎΠ½ Π½Π΅ ΡΠ°Π±ΠΎΡΠ°Π΅Ρ: some_widget.config(image=PhotoImage(file=test.png), compound=RIGHT) Π½ΠΎ ΡΡΠΎ ΡΠ°Π±ΠΎΡΠ°Π΅Ρ.
Π― ΠΈΡΠΏΠΎΠ»ΡΠ·ΡΡ python 3 ΠΈ tkinter. Π― Ρ ΠΎΡΡ ΠΏΠΎΠΌΠ΅ΡΡΠΈΡΡ ΡΠ²ΠΎΠ΅ ΡΠΎΠ±ΡΡΠ²Π΅Π½Π½ΠΎΠ΅ ΠΈΠ·ΠΎΠ±ΡΠ°ΠΆΠ΅Π½ΠΈΠ΅ Π² ΠΊΠ°ΡΠ΅ΡΡΠ²Π΅ Π·Π½Π°ΡΠΊΠ° Π² ΠΊΠΎΡΠ½Π΅Π²ΠΎΠ΅ ΠΎΠΊΠ½ΠΎ Π²ΠΌΠ΅ΡΡΠΎ Π·Π½Π°ΡΠΊΠ° ΠΏΠΎ ΡΠΌΠΎΠ»ΡΠ°Π½ΠΈΡ tkinter. ΠΠΎΠΆΠ°Π»ΡΠΉΡΡΠ°, ΠΏΠΎΠΌΠΎΠ³ΠΈΡΠ΅ ΠΌΠ½Π΅. Π― ΠΏΡΠΎΠ±ΠΎΠ²Π°Π» ΡΡΠΎΡ ΠΊΠΎΠ΄, Π½ΠΎ.
Π― ΡΠΎΠ·Π΄Π°Π» ΠΏΡΠΎΡΡΡΡ ΠΏΡΠΎΠ³ΡΠ°ΠΌΠΌΡ ΠΎΡΠΊΡΡΡΠΈΡ ΠΈΠ·ΠΎΠ±ΡΠ°ΠΆΠ΅Π½ΠΈΠΉ, ΠΊΠΎΡΠΎΡΠ°Ρ ΠΎΡΠΊΡΡΠ²Π°Π΅Ρ ΠΈΠ·ΠΎΠ±ΡΠ°ΠΆΠ΅Π½ΠΈΠ΅, Π²ΡΠ±ΡΠ°Π½Π½ΠΎΠ΅ ΠΈΠ· filedialog, Π½Π°ΠΆΠ°Π² ΠΊΠ½ΠΎΠΏΠΊΡ, Π½ΠΎ Π²Π΅Π·Π΄Π΅, Π³Π΄Π΅ Ρ Π²ΡΠ±ΠΈΡΠ°Ρ Π΄ΡΡΠ³ΠΎΠ΅ ΠΈΠ·ΠΎΠ±ΡΠ°ΠΆΠ΅Π½ΠΈΠ΅, ΠΎΠ½ΠΎ ΠΏΡΠΎΡΡΠΎ ΠΏΠΎΡΠ²Π»ΡΠ΅ΡΡΡ ΠΏΠΎΠ΄ ΡΠ΅ΠΊΡΡΠΈΠΌ.
Π― ΠΏΡΡΠ°ΡΡΡ ΡΠΎΠ·Π΄Π°ΡΡ ΠΏΡΠΈΠ»ΠΎΠΆΠ΅Π½ΠΈΠ΅ ΠΏΠΎΠ³ΠΎΠ΄Ρ Π² Tkinter GUI. ΠΠ½ ΠΏΡΠ΅ΠΊΡΠ°ΡΠ½ΠΎ ΡΠ°Π±ΠΎΡΠ°Π΅Ρ. Π― Ρ ΠΎΡΡ Π΄ΠΎΠ±Π°Π²ΠΈΡΡ ΠΊ Π½Π΅ΠΌΡ ΡΠΎΠ½ΠΎΠ²ΠΎΠ΅ ΠΈΠ·ΠΎΠ±ΡΠ°ΠΆΠ΅Π½ΠΈΠ΅, Π½ΠΎ ΠΎΠ½ΠΎ Π½Π΅ ΠΏΠΎΡΠ²Π»ΡΠ΅ΡΡΡ. ΡΠ½Π°ΡΠ°Π»Π° Ρ ΡΠ΄Π΅Π»Π°Π» ΡΡΠΎ Π±Π΅Π· ΠΎΠΏΠΎΡΠ½ΠΎΠΉ Π»ΠΈΠ½ΠΈΠΈ, Π½ΠΎ ΡΡΠΎ Π½Π΅.
Python Tkinter Image + Examples
In this Python tutorial, we will learn how to add image in Python Tkinter. let us start the Python Tkinter Image with below examples.
Python Tkinter Image
Python Tkinter has the method PhotoImage which allows reading images in Python Tkinter. And then Image can be placed by providing adding PhotoImage variable in image property of widgets like Label, Button, Frame, etc.
Code
This is the basic code to demonstrate how to add images in Python Tkinter. Since the below code is just to display an image so we have used the PhotoImage method in Python Tkinter.
Output
In this output, image is displayed using label widget and since we have not provided any geometry so the application size is to the size of image.
Python Tkinter Image Display
Image in Python Tkinter can be displayed either by using the PhotoImage module or by using the Pillow library.
Code using PhotoImage method
In this code, you can observe that we have not imported any libraries as PhotoImage automatically loaded when everything is imported.
Output of PhotoImage method
The image is displayed on the canvas and it is showing incomplete because we have provided height and width of canvas less than the size of image.
Code using Pillow Library
In this code, we have imported ImageTk, the Image method from the PIL library. PIL is the short name for a pillow. Though the code looks much similar to the previous one now we can read more image extensions like jpg, png, bitmap, etc.
Output Of image displayed using Pillow Library
The output is similar to the PhotoImage section, and photo is incomplete because the size of the window provided is less than the original size of image.
Python Tkinter Image Button
Button widget in Python Tkinter has image property, by providing image variable we can place image on the button widget.
In this section, we will learn how to put image on the button in Python Tkinter.
Code
In this code, we have added an image on the button. User can click on the image to perform the action.
Output
In this output, Download image is added on the button. Users can click on this image to download the file.
Python Tkinter Image Background
In this section, we will learn how to insert a background image in Python Tkinter. In other words, how to set background image in Python Tkinter.
Code
In this code, we have imported webbrowser so that we can open the webpage. We have placed image in the background using canvas method create_image and then using create _text and create_window we have placed the button and text on the window.
Output
In this output, you can see that application has background image, text and button. When user will click on the button he/she will be redirected to a website.
Python Tkinter Image Resize
In this section, we will learn how to resize the image in Python Tkinter.
Code
In this code, we have used Image method from Pillow module to change the size of the image.
Output
In this output, you can see that image is resizing as per the height and width provided by the user. The is a full fledged application created in Python Tkinter that can be used for daily activities.
Python Tkinter Image Size
In this section, we will learn how to get the image size in Python Tkinter.
Code
In this code, text=f’width: height:
‘ this command displays the height and width of the image. Here, img is the variable that stores the file file.
Output
In this output, you can see that height and width of the image is displayed at the bottom of the page.
Python Tkinter Image Label
In this section, we will learn how to set image on the Label widget in Python Tkinter.
Code
Here is the simple code to display image using Label widget in Python Tkinter.
Output
Python Tkinter Image Doesnβt Exist
This is a common error that is encountered by almost all the programmers working with images in Python Tkinter that says, Python Tkinter Image Doesnβt Exist.
You may like the following Python tkinter tutorials:
In this tutorial, we have learned how to add images in Python Tkinter. Also, we have covered these topics.
Entrepreneur, Founder, Author, Blogger, Trainer, and more. Check out my profile.
Python | ΠΠΎΠ±Π°Π²ΠΈΡΡ ΠΈΠ·ΠΎΠ±ΡΠ°ΠΆΠ΅Π½ΠΈΠ΅ Π½Π° ΠΊΠ½ΠΎΠΏΠΊΡ Tkinter
Tkinter β ΡΡΠΎ ΠΌΠΎΠ΄ΡΠ»Ρ Python, ΠΊΠΎΡΠΎΡΡΠΉ ΠΈΡΠΏΠΎΠ»ΡΠ·ΡΠ΅ΡΡΡ Π΄Π»Ρ ΡΠΎΠ·Π΄Π°Π½ΠΈΡ ΠΏΡΠΈΠ»ΠΎΠΆΠ΅Π½ΠΈΠΉ Ρ Π³ΡΠ°ΡΠΈΡΠ΅ΡΠΊΠΈΠΌ ΠΈΠ½ΡΠ΅ΡΡΠ΅ΠΉΡΠΎΠΌ ΠΏΠΎΠ»ΡΠ·ΠΎΠ²Π°ΡΠ΅Π»Ρ Ρ ΠΏΠΎΠΌΠΎΡΡΡ ΡΠ°Π·Π½ΠΎΠΎΠ±ΡΠ°Π·Π½ΡΡ Π²ΠΈΠ΄ΠΆΠ΅ΡΠΎΠ² ΠΈ ΡΡΠ½ΠΊΡΠΈΠΉ. ΠΠ°ΠΊ ΠΈ Π»ΡΠ±ΠΎΠΉ Π΄ΡΡΠ³ΠΎΠΉ ΠΌΠΎΠ΄ΡΠ»Ρ GUI, ΠΎΠ½ ΡΠ°ΠΊΠΆΠ΅ ΠΏΠΎΠ΄Π΄Π΅ΡΠΆΠΈΠ²Π°Π΅Ρ ΠΈΠ·ΠΎΠ±ΡΠ°ΠΆΠ΅Π½ΠΈΡ, Ρ.Π΅. Π²Ρ ΠΌΠΎΠΆΠ΅ΡΠ΅ ΠΈΡΠΏΠΎΠ»ΡΠ·ΠΎΠ²Π°ΡΡ ΠΈΠ·ΠΎΠ±ΡΠ°ΠΆΠ΅Π½ΠΈΡ Π² ΠΏΡΠΈΠ»ΠΎΠΆΠ΅Π½ΠΈΠΈ, ΡΡΠΎΠ±Ρ ΡΠ΄Π΅Π»Π°ΡΡ Π΅Π³ΠΎ Π±ΠΎΠ»Π΅Π΅ ΠΏΡΠΈΠ²Π»Π΅ΠΊΠ°ΡΠ΅Π»ΡΠ½ΡΠΌ.
ΠΠ°ΠΆΠ½ΠΎΠ΅ Π·Π°ΠΌΠ΅ΡΠ°Π½ΠΈΠ΅: ΠΡΠ»ΠΈ Π½Π° ΠΊΠ½ΠΎΠΏΠΊΠ΅ ΡΠΊΠ°Π·Π°Π½Ρ ΠΊΠ°ΠΊ ΠΈΠ·ΠΎΠ±ΡΠ°ΠΆΠ΅Π½ΠΈΠ΅, ΡΠ°ΠΊ ΠΈ ΡΠ΅ΠΊΡΡ, ΡΠ΅ΠΊΡΡ Π±ΡΠ΄Π΅Ρ ΠΏΡΠ΅ΠΎΠ±Π»Π°Π΄Π°ΡΡ, ΠΈ Π½Π° ΠΊΠ½ΠΎΠΏΠΊΠ΅ Π±ΡΠ΄Π΅Ρ ΠΎΡΠΎΠ±ΡΠ°ΠΆΠ°ΡΡΡΡ ΡΠΎΠ»ΡΠΊΠΎ ΠΈΠ·ΠΎΠ±ΡΠ°ΠΆΠ΅Π½ΠΈΠ΅. ΠΠΎ Π΅ΡΠ»ΠΈ Π²Ρ Ρ ΠΎΡΠΈΡΠ΅ ΠΏΠΎΠΊΠ°Π·Π°ΡΡ ΠΊΠ°ΠΊ ΠΈΠ·ΠΎΠ±ΡΠ°ΠΆΠ΅Π½ΠΈΠ΅, ΡΠ°ΠΊ ΠΈ ΡΠ΅ΠΊΡΡ, Π²Ρ Π΄ΠΎΠ»ΠΆΠ½Ρ ΠΏΠ΅ΡΠ΅Π΄Π°ΡΡ ΡΠΎΡΡΠ°Π²Π½ΡΠ΅ Π² Π½Π°ΡΡΡΠΎΠΉΠΊΠ°Ρ ΠΊΠ½ΠΎΠΏΠΊΠΈ.
Button(master, text = «Button», image = «image.png», compound=LEFT)
Π‘ΠΈΠ½ΡΠ°ΠΊΡΠΈΡ:
Π»ΡΠ±ΠΎΠΉ Π΄ΠΎΠΏΡΡΡΠΈΠΌΡΠΉ ΠΏΡΡΡ, Π΄ΠΎΡΡΡΠΏΠ½ΡΠΉ Π½Π° Π²Π°ΡΠ΅ΠΌ Π»ΠΎΠΊΠ°Π»ΡΠ½ΠΎΠΌ ΠΊΠΎΠΌΠΏΡΡΡΠ΅ΡΠ΅.
ΠΠΎΠ΄ β 1:
# ΠΈΠΌΠΏΠΎΡΡ ΡΠΎΠ»ΡΠΊΠΎ ΡΡΠΈΡ
ΡΡΠ½ΠΊΡΠΈΠΉ
# ΠΊΠΎΡΠΎΡΡΠ΅ Π½ΡΠΆΠ½Ρ
from tkinter import *
from tkinter.ttk import *
# ΡΠΎΠ·Π΄Π°Π½ΠΈΠ΅ ΠΎΠΊΠ½Π° tkinter
# ΠΠΎΠ±Π°Π²Π»Π΅Π½ΠΈΠ΅ Π²ΠΈΠ΄ΠΆΠ΅ΡΠΎΠ² Π² ΠΊΠΎΡΠ½Π΅Π²ΠΎΠ΅ ΠΎΠΊΠ½ΠΎ
# Π‘ΠΎΠ·Π΄Π°Π½ΠΈΠ΅ ΠΎΠ±ΡΠ΅ΠΊΡΠ° ΡΠΎΡΠΎΠΈΠ·ΠΎΠ±ΡΠ°ΠΆΠ΅Π½ΠΈΡ Π΄Π»Ρ ΠΈΡΠΏΠΎΠ»ΡΠ·ΠΎΠ²Π°Π½ΠΈΡ ΠΈΠ·ΠΎΠ±ΡΠ°ΠΆΠ΅Π½ΠΈΡ
photo = PhotoImage( file = r «C:\Gfg\circle.png» )
# Π·Π΄Π΅ΡΡ, ΠΎΠΏΡΠΈΡ ΠΈΠ·ΠΎΠ±ΡΠ°ΠΆΠ΅Π½ΠΈΡ ΠΈΡΠΏΠΎΠ»ΡΠ·ΡΠ΅ΡΡΡ Π΄Π»Ρ
# ΡΡΡΠ°Π½ΠΎΠ²ΠΈΡΡ ΠΈΠ·ΠΎΠ±ΡΠ°ΠΆΠ΅Π½ΠΈΠ΅ Π½Π° ΠΊΠ½ΠΎΠΏΠΊΡ
ΠΡΡ
ΠΎΠ΄: 
ΠΡΠΈ Π²ΡΠ²ΠΎΠ΄Π΅ ΠΎΠ±ΡΠ°ΡΠΈΡΠ΅ Π²Π½ΠΈΠΌΠ°Π½ΠΈΠ΅, ΡΡΠΎ Π½Π° ΠΊΠ½ΠΎΠΏΠΊΠ΅ ΠΎΡΠΎΠ±ΡΠ°ΠΆΠ°Π΅ΡΡΡ ΡΠΎΠ»ΡΠΊΠΎ ΠΈΠ·ΠΎΠ±ΡΠ°ΠΆΠ΅Π½ΠΈΠ΅, Π° ΡΠ°Π·ΠΌΠ΅Ρ ΠΊΠ½ΠΎΠΏΠΊΠΈ ΡΠ°ΠΊΠΆΠ΅ Π±ΠΎΠ»ΡΡΠ΅ ΠΎΠ±ΡΡΠ½ΠΎΠ³ΠΎ ΡΠ°Π·ΠΌΠ΅ΡΠ°, ΠΏΠΎΡΠΎΠΌΡ ΡΡΠΎ ΠΌΡ Π½Π΅ ΡΡΡΠ°Π½ΠΎΠ²ΠΈΠ»ΠΈ ΡΠ°Π·ΠΌΠ΅Ρ ΠΈΠ·ΠΎΠ±ΡΠ°ΠΆΠ΅Π½ΠΈΡ.
# ΠΈΠΌΠΏΠΎΡΡ ΡΠΎΠ»ΡΠΊΠΎ ΡΡΠΈΡ
ΡΡΠ½ΠΊΡΠΈΠΉ
# ΠΊΠΎΡΠΎΡΡΠ΅ Π½ΡΠΆΠ½Ρ
from tkinter import *
from tkinter.ttk import *
# ΡΠΎΠ·Π΄Π°Π½ΠΈΠ΅ ΠΎΠΊΠ½Π° tkinter
# ΠΠΎΠ±Π°Π²Π»Π΅Π½ΠΈΠ΅ Π²ΠΈΠ΄ΠΆΠ΅ΡΠΎΠ² Π² ΠΊΠΎΡΠ½Π΅Π²ΠΎΠ΅ ΠΎΠΊΠ½ΠΎ
# Π‘ΠΎΠ·Π΄Π°Π½ΠΈΠ΅ ΠΎΠ±ΡΠ΅ΠΊΡΠ° ΡΠΎΡΠΎΠΈΠ·ΠΎΠ±ΡΠ°ΠΆΠ΅Π½ΠΈΡ Π΄Π»Ρ ΠΈΡΠΏΠΎΠ»ΡΠ·ΠΎΠ²Π°Π½ΠΈΡ ΠΈΠ·ΠΎΠ±ΡΠ°ΠΆΠ΅Π½ΠΈΡ
photo = PhotoImage( file = r «C:\Gfg\circle.png» )
# ΠΠ·ΠΌΠ΅Π½Π΅Π½ΠΈΠ΅ ΡΠ°Π·ΠΌΠ΅ΡΠ° ΠΈΠ·ΠΎΠ±ΡΠ°ΠΆΠ΅Π½ΠΈΡ Π΄Π»Ρ ΡΠ°Π·ΠΌΠ΅ΡΠ΅Π½ΠΈΡ Π½Π° ΠΊΠ½ΠΎΠΏΠΊΠ΅
# Π·Π΄Π΅ΡΡ, ΠΎΠΏΡΠΈΡ ΠΈΠ·ΠΎΠ±ΡΠ°ΠΆΠ΅Π½ΠΈΡ ΠΈΡΠΏΠΎΠ»ΡΠ·ΡΠ΅ΡΡΡ Π΄Π»Ρ
# ΡΡΡΠ°Π½ΠΎΠ²ΠΈΡΡ ΠΈΠ·ΠΎΠ±ΡΠ°ΠΆΠ΅Π½ΠΈΠ΅ Π½Π° ΠΊΠ½ΠΎΠΏΠΊΡ
# ΡΠΎΡΡΠ°Π²Π½Π°Ρ ΠΎΠΏΡΠΈΡ ΠΈΡΠΏΠΎΠ»ΡΠ·ΡΠ΅ΡΡΡ Π΄Π»Ρ Π²ΡΡΠ°Π²Π½ΠΈΠ²Π°Π½ΠΈΡ
# ΠΈΠ·ΠΎΠ±ΡΠ°ΠΆΠ΅Π½ΠΈΠ΅ Π½Π° Π»Π΅Π²ΠΎΠΉ ΡΡΠΎΡΠΎΠ½Π΅ ΠΊΠ½ΠΎΠΏΠΊΠΈ
compound = LEFT).pack(side = TOP)
ΠΡΡ
ΠΎΠ΄: 
ΠΠ±ΡΠ°ΡΠΈΡΠ΅ Π²Π½ΠΈΠΌΠ°Π½ΠΈΠ΅, ΡΡΠΎ ΠΊΠ°ΠΊ ΡΠ΅ΠΊΡΡ, ΡΠ°ΠΊ ΠΈ ΠΈΠ·ΠΎΠ±ΡΠ°ΠΆΠ΅Π½ΠΈΠ΅ ΠΏΠΎΡΠ²Π»ΡΡΡΡΡ, Π° ΡΠ°Π·ΠΌΠ΅Ρ ΠΈΠ·ΠΎΠ±ΡΠ°ΠΆΠ΅Π½ΠΈΡ ΡΠ°ΠΊΠΆΠ΅ Π½Π΅Π²Π΅Π»ΠΈΠΊ.
How to add an image in Tkinter?
How do I add an image in Tkinter?
This gave me a syntax error:
9 Answers 9
Python 3.3.1 [MSC v.1600 32 bit (Intel)] on win32 14.May.2013
This worked for me, by following the code above
The example above worked fine for me, testing on the interactive interpreter.
Here is an example for Python 3 that you can edit for Python 2 π
Following code works on my machine
make sure you have PIL package installed
Below is an example displaying:
It’s not a standard lib of python 2.7. So in order for these to work properly and if you’re using Python 2.7 you should download the PIL library first: Direct download link: http://effbot.org/downloads/PIL-1.1.7.win32-py2.7.exe After installing it, follow these steps:
Edit your script.py
It’s a Python version problem. If you are using the latest, then your old syntax won’t work and give you this error. Please follow @Josav09’s code and you will be fine.
Just convert the jpg format image into png format. It will work 100%.
Not the answer you’re looking for? Browse other questions tagged python user-interface tkinter or ask your own question.
Linked
Related
Hot Network Questions
Subscribe to RSS
To subscribe to this RSS feed, copy and paste this URL into your RSS reader.
site design / logo Β© 2021 Stack Exchange Inc; user contributions licensed under cc by-sa. rev 2021.12.16.41042
By clicking βAccept all cookiesβ, you agree Stack Exchange can store cookies on your device and disclose information in accordance with our Cookie Policy.
ΠΠ°Π³ΡΡΠ·ΠΊΠ° ΠΈΠ·ΠΎΠ±ΡΠ°ΠΆΠ΅Π½ΠΈΠΉ Π² Tkinter Ρ ΠΈΡΠΏΠΎΠ»ΡΠ·ΠΎΠ²Π°Π½ΠΈΠ΅ΠΌ PIL
Π ΡΡΠΎΠΉ ΡΡΠ°ΡΡΠ΅ ΠΌΡ Π½Π°ΡΡΠΈΠΌΡΡ Π·Π°Π³ΡΡΠΆΠ°ΡΡ ΠΈΠ·ΠΎΠ±ΡΠ°ΠΆΠ΅Π½ΠΈΡ ΠΈΠ· ΠΏΠΎΠ»ΡΠ·ΠΎΠ²Π°ΡΠ΅Π»ΡΡΠΊΠΎΠΉ ΡΠΈΡΡΠ΅ΠΌΡ Π² ΠΎΠΊΠ½ΠΎ Tkinter Ρ ΠΏΠΎΠΌΠΎΡΡΡ ΠΌΠΎΠ΄ΡΠ»Ρ PIL. ΠΡΠ° ΠΏΡΠΎΠ³ΡΠ°ΠΌΠΌΠ° ΠΎΡΠΊΡΠΎΠ΅Ρ Π΄ΠΈΠ°Π»ΠΎΠ³ΠΎΠ²ΠΎΠ΅ ΠΎΠΊΠ½ΠΎ, ΡΡΠΎΠ±Ρ Π²ΡΠ±ΡΠ°ΡΡ Π½ΡΠΆΠ½ΡΠΉ ΡΠ°ΠΉΠ» ΠΈΠ· Π»ΡΠ±ΠΎΠ³ΠΎ ΠΊΠ°ΡΠ°Π»ΠΎΠ³Π° ΠΈ ΠΎΡΠΎΠ±ΡΠ°Π·ΠΈΡΡ Π΅Π³ΠΎ Π² ΠΎΠΊΠ½Π΅ tkinter.
Π£ΡΡΠ°Π½ΠΎΠ²ΠΈΡΠ΅ ΡΡΠ΅Π±ΠΎΠ²Π°Π½ΠΈΡ β
ΠΡΠΏΠΎΠ»ΡΠ·ΡΠΉΡΠ΅ ΡΡΡ ΠΊΠΎΠΌΠ°Π½Π΄Ρ Π΄Π»Ρ ΡΡΡΠ°Π½ΠΎΠ²ΠΊΠΈ Tkinter:
ΠΡΠΏΠΎΠ»ΡΠ·ΡΠΉΡΠ΅ ΡΡΡ ΠΊΠΎΠΌΠ°Π½Π΄Ρ Π΄Π»Ρ ΡΡΡΠ°Π½ΠΎΠ²ΠΊΠΈ PIL:
ΠΠΌΠΏΠΎΡΡ ΠΌΠΎΠ΄ΡΠ»Π΅ΠΉ β
from tkinter import *
# Π·Π°Π³ΡΡΠ·ΠΊΠ° Π±ΠΈΠ±Π»ΠΈΠΎΡΠ΅ΠΊΠΈ ΠΈΠ·ΠΎΠ±ΡΠ°ΠΆΠ΅Π½ΠΈΠΉ Python
from PIL import ImageTk, Image
# Π§ΡΠΎΠ±Ρ Π΄ΠΈΠ°Π»ΠΎΠ³ΠΎΠ²ΠΎΠ΅ ΠΎΠΊΠ½ΠΎ ΠΎΡΠΊΡΡΠ²Π°Π»ΠΎΡΡ ΠΏΡΠΈ Π½Π΅ΠΎΠ±Ρ ΠΎΠ΄ΠΈΠΌΠΎΡΡΠΈ
from tkinter import filedialog
ΠΡΠΈΠΌΠ΅ΡΠ°Π½ΠΈΠ΅. ΠΠΎΠ΄ΡΠ»Ρ ImageTk ΠΏΠΎΠ΄Π΄Π΅ΡΠΆΠΈΠ²Π°Π΅Ρ ΡΠΎΠ·Π΄Π°Π½ΠΈΠ΅ ΠΈ ΠΈΠ·ΠΌΠ΅Π½Π΅Π½ΠΈΠ΅ ΠΎΠ±ΡΠ΅ΠΊΡΠΎΠ² Tkinter BitmapImage ΠΈ PhotoImage ΠΈΠ· ΠΈΠ·ΠΎΠ±ΡΠ°ΠΆΠ΅Π½ΠΈΠΉ PIL, Π° filedialog ΠΈΡΠΏΠΎΠ»ΡΠ·ΡΠ΅ΡΡΡ Π΄Π»Ρ filedialog Π΄ΠΈΠ°Π»ΠΎΠ³ΠΎΠ²ΠΎΠ³ΠΎ ΠΎΠΊΠ½Π°, ΠΊΠΎΠ³Π΄Π° Π²Ρ ΠΎΡΠΊΡΡΠ²Π°Π΅ΡΠ΅ ΡΠ°ΠΉΠ» ΠΈΠ· Π»ΡΠ±ΠΎΠΉ ΡΠΎΡΠΊΠΈ Π²Π°ΡΠ΅ΠΉ ΡΠΈΡΡΠ΅ΠΌΡ ΠΈΠ»ΠΈ ΡΠΎΡ
ΡΠ°Π½ΡΠ΅ΡΠ΅ ΡΠ°ΠΉΠ» Π² ΠΎΠΏΡΠ΅Π΄Π΅Π»Π΅Π½Π½ΠΎΠΉ ΠΏΠΎΠ·ΠΈΡΠΈΠΈ ΠΈΠ»ΠΈ ΠΌΠ΅ΡΡΠ΅.
Π€ΡΠ½ΠΊΡΠΈΡ Π΄Π»Ρ ΡΠΎΠ·Π΄Π°Π½ΠΈΡ ΠΎΠΊΠ½Π° Tkinder, ΡΠΎΡΡΠΎΡΡΠ΅Π³ΠΎ ΠΈΠ· ΠΊΠ½ΠΎΠΏΠΊΠΈ β
# Π£ΡΡΠ°Π½ΠΎΠ²ΠΈΡΡ Π·Π°Π³ΠΎΠ»ΠΎΠ²ΠΎΠΊ Π² ΠΊΠ°ΡΠ΅ΡΡΠ²Π΅ Π·Π°Π³ΡΡΠ·ΡΠΈΠΊΠ° ΠΈΠ·ΠΎΠ±ΡΠ°ΠΆΠ΅Π½ΠΈΠΉ
root.title( «Image Loader» )
# Π£ΡΡΠ°Π½ΠΎΠ²ΠΈΡΠ΅ ΡΠ°Π·ΡΠ΅ΡΠ΅Π½ΠΈΠ΅ ΠΎΠΊΠ½Π°
root.geometry( «550×300 + 300 + 150» )
# Π Π°Π·ΡΠ΅ΡΠΈΡΡ ΠΈΠ·ΠΌΠ΅Π½Π΅Π½ΠΈΠ΅ ΡΠ°Π·ΠΌΠ΅ΡΠ° ΠΎΠΊΠ½Π°
# Π‘ΠΎΠ·Π΄Π°ΠΉΡΠ΅ ΠΊΠ½ΠΎΠΏΠΊΡ ΠΈ ΠΏΠΎΠΌΠ΅ΡΡΠΈΡΠ΅ Π΅Π΅ Π² ΠΎΠΊΠ½ΠΎ, ΠΈΡΠΏΠΎΠ»ΡΠ·ΡΡ ΡΠ΅ΡΠΊΡ
Π€ΡΠ½ΠΊΡΠΈΡ Π΄Π»Ρ ΡΠ°Π·ΠΌΠ΅ΡΠ΅Π½ΠΈΡ ΠΈΠ·ΠΎΠ±ΡΠ°ΠΆΠ΅Π½ΠΈΡ Π½Π° ΠΎΠΊΠ½Π΅ β
# ΠΡΠ±Π΅ΡΠΈΡΠ΅ Imagename ΠΈΠ· ΠΏΠ°ΠΏΠΊΠΈ
# ΠΈΠ·ΠΌΠ΅Π½ΠΈΡΡ ΡΠ°Π·ΠΌΠ΅Ρ ΠΈΠ·ΠΎΠ±ΡΠ°ΠΆΠ΅Π½ΠΈΡ ΠΈ ΠΏΡΠΈΠΌΠ΅Π½ΠΈΡΡ Π²ΡΡΠΎΠΊΠΎΠΊΠ°ΡΠ΅ΡΡΠ²Π΅Π½Π½ΡΠΉ ΡΠΈΠ»ΡΡΡ ΡΠ³Π»Π°ΠΆΠΈΠ²Π°Π½ΠΈΡ
# ΠΠ»Π°ΡΡ PhotoImage ΠΈΡΠΏΠΎΠ»ΡΠ·ΡΠ΅ΡΡΡ Π΄Π»Ρ Π΄ΠΎΠ±Π°Π²Π»Π΅Π½ΠΈΡ ΠΈΠ·ΠΎΠ±ΡΠ°ΠΆΠ΅Π½ΠΈΡ Π² Π²ΠΈΠ΄ΠΆΠ΅ΡΡ, ΠΈΠΊΠΎΠ½ΠΊΠΈ ΠΈ Ρ. Π.
panel = Label(root, image = img)
# ΡΡΡΠ°Π½ΠΎΠ²ΠΈΡΡ ΠΈΠ·ΠΎΠ±ΡΠ°ΠΆΠ΅Π½ΠΈΠ΅ ΠΊΠ°ΠΊ img
Π€ΡΠ½ΠΊΡΠΈΡ openfilename Π²Π΅ΡΠ½Π΅Ρ ΠΈΠΌΡ ΡΠ°ΠΉΠ»Π° ΠΈΠ·ΠΎΠ±ΡΠ°ΠΆΠ΅Π½ΠΈΡ.
Π€ΡΠ½ΠΊΡΠΈΡ Π΄Π»Ρ Π²ΠΎΠ·Π²ΡΠ°ΡΠ° ΠΈΠΌΠ΅Π½ΠΈ ΡΠ°ΠΉΠ»Π°, Π²ΡΠ±ΡΠ°Π½Π½ΠΎΠ³ΠΎ ΠΈΠ· Π΄ΠΈΠ°Π»ΠΎΠ³ΠΎΠ²ΠΎΠ³ΠΎ ΠΎΠΊΠ½Π° β
# ΠΎΡΠΊΡΡΡΡ ΡΠ°ΠΉΠ» Π΄ΠΈΠ°Π»ΠΎΠ³ΠΎΠ²ΠΎΠ΅ ΠΎΠΊΠ½ΠΎ Π΄Π»Ρ Π²ΡΠ±ΠΎΡΠ° ΠΈΠ·ΠΎΠ±ΡΠ°ΠΆΠ΅Π½ΠΈΡ
# ΠΠΈΠ°Π»ΠΎΠ³ΠΎΠ²ΠΎΠ΅ ΠΎΠΊΠ½ΠΎ ΠΈΠΌΠ΅Π΅Ρ Π·Π°Π³ΠΎΠ»ΠΎΠ²ΠΎΠΊ Β«ΠΡΠΊΡΡΡΡΒ»
filename = filedialog.askopenfilename(title = ‘»pen’ )









