Derniers messages sur Zeste de Savoirhttps://zestedesavoir.com/forums/2024-03-28T18:46:23+01:00Les derniers messages parus sur le forum de Zeste de Savoir.Ne pas perdre le focus lors d'un appel a messagebox.showinfo (tkinter), message #2544342024-03-28T18:46:23+01:00wemosd1/@wemosd1https://zestedesavoir.com/forums/sujet/17385/ne-pas-perdre-le-focus-lors-dun-appel-a-messageboxshowinfo-tkinter/?page=1#p254434<p>Oui c’est sa, j’ai entre temps trouvé la solution:</p>
<p>je fais un truc comme ceci et sa marche:</p>
<div class="hljs-code-div hljs-code-py"><div class="hljs-line-numbers"><span data-count="1"></span><span data-count="2"></span></div><pre><code class="hljs language-py"> messagebox.showinfo(<span class="hljs-string">"info"</span>, <span class="hljs-string">"Configuration enregistrée"</span>,parent = new_window_add_plate)
</code></pre></div>
<p>Avec parent = la fenetre en cours</p>
<p>Au sujet de master j’ai rien trouvé a ce sujet</p>Ne pas perdre le focus lors d'un appel a messagebox.showinfo (tkinter), message #2544322024-03-28T16:52:51+01:00entwanne/@entwannehttps://zestedesavoir.com/forums/sujet/17385/ne-pas-perdre-le-focus-lors-dun-appel-a-messageboxshowinfo-tkinter/?page=1#p254432<p>J’ai l’impression que le souci vient du fait que tu as une fenêtre mère (« Test ») et une fenêtre fille (« Configuration ») et que la <em>messagebox</em> dépend de la mère et non de la fille (donc redonne le focus à la mère).</p>
<p>Je ne suis pas sûr de reproduire le problème chez moi, mais s’il s’agit bien de ça alors tu pourrais donner un argument <code>master=...</code> à la <em>messagebox</em> pour choisir de quelle fenêtre elle dépend.</p>Ne pas perdre le focus lors d'un appel a messagebox.showinfo (tkinter), message #2544312024-03-28T15:33:58+01:00wemosd1/@wemosd1https://zestedesavoir.com/forums/sujet/17385/ne-pas-perdre-le-focus-lors-dun-appel-a-messageboxshowinfo-tkinter/?page=1#p254431<p>Bonjour, je suis en train de faire un petit programme qui utilise tkinter.</p>
<p>Je rencontre un problème de perte de focus sur ma fenetre nommé new_windows.</p>
<p>En fait j’appelle</p>
<p><code>messagebox.showinfo("info","mon message")</code> </p>
<p>Je vais essayer d’expliquer le problème:</p>
<p>J’ouvre mon programme, ma fenêtre principale s’ouvre, je clique sur configuration, ma fenêtre configuration s’ouvre bien.</p>
<p>A un moment donné j’ouvre un messagebox et la je perds le focus sur ma page configuration.</p>
<p>Je crée ma fenêtre comme ceci:</p>
<div class="hljs-code-div hljs-code-py"><div class="hljs-line-numbers"><span data-count="1"></span><span data-count="2"></span><span data-count="3"></span><span data-count="4"></span></div><pre><code class="hljs language-py"> new_window_add_plate = Toplevel(root)
new_window_add_plate.grab_set() <span class="hljs-comment"># garder le focus sur la fenetre</span>
new_window_add_plate.title(<span class="hljs-string">"Ajouter plaque a la liste noire"</span>)
new_window_add_plate.geometry(<span class="hljs-string">"640x480"</span>) <span class="hljs-comment"># Définir la taille de la nouvelle fenêtre`</span>
</code></pre></div>
<p>J’ai trouvé une parade en utilisant:</p>
<div class="hljs-code-div hljs-code-py"><pre><code class="hljs language-py">new_window_add_plate.grab_set()
</code></pre></div>
<p>Se qui fonctionne en partie car ma fenêtre configuration perd le focus et affiche le messagebox au dessus de ma fenêtre principale.</p>
<p>Lorsque je valide le checkbox je recupere le focus sur la fenêtre de configuration.</p>
<p>Auriez vous une idée sur comment éviter de perdre le focus?</p>
<p>Voici une 2 images plus parlantes:</p>
<p>Perte de focus</p>
<figure><img src="https://i.ibb.co/bzYbHrQ/bug.png" alt="Image utilisateur" loading="lazy"><figcaption>Image utilisateur</figcaption></figure>
<p>Recuperation du focus:</p>
<figure><img src="https://i.ibb.co/H40MFQf/bug2.png" alt="Image utilisateur" loading="lazy"><figcaption>Image utilisateur</figcaption></figure>Python appel de coordonnées tkinter, message #2527742023-11-07T18:56:38+01:00ache/@achehttps://zestedesavoir.com/forums/sujet/17200/python-appel-de-coordonnees-tkinter/?page=1#p252774<p>Le problème principal est répondu il faut utiliser <code>*_</code> plutôt que <code>_</code> si tu veux ignorer plusieurs arguments.</p>
<p>Si ton problème est plus général, on aura besoin de plus de contexte. Notamment la structure générale de ton code et ça nous simplifierait la vie que tu nous décrives ce que tu veux faire.</p>
<p>Pour l’instant la classe <code>attaquer</code> est très abstrait pour nous, on ne peut rien répondre sans plus de contexte.</p>Python appel de coordonnées tkinter, message #2527722023-11-07T18:17:18+01:00fred1599/@fred1599https://zestedesavoir.com/forums/sujet/17200/python-appel-de-coordonnees-tkinter/?page=1#p252772<p>Bonjour,</p>
<p>La méthode coords, retourne 4 valeurs, il faut faire des tests sur son interpréteur</p>
<div class="hljs-code-div hljs-code-py"><div class="hljs-line-numbers"><span data-count="1"></span><span data-count="2"></span><span data-count="3"></span><span data-count="4"></span><span data-count="5"></span><span data-count="6"></span><span data-count="7"></span><span data-count="8"></span></div><pre><code class="hljs language-py"><span class="hljs-meta">>>> </span>t = (<span class="hljs-number">1</span>, <span class="hljs-number">2</span>, <span class="hljs-number">3</span>, <span class="hljs-number">4</span>)
<span class="hljs-meta">>>> </span>a, _ = t
Traceback (most recent call last):
File <span class="hljs-string">"<stdin>"</span>, line <span class="hljs-number">1</span>, <span class="hljs-keyword">in</span> <module>
ValueError: too many values to unpack (expected <span class="hljs-number">2</span>)
<span class="hljs-meta">>>> </span>a, *_ = t
<span class="hljs-meta">>>> </span>a
<span class="hljs-number">1</span>
</code></pre></div>Python appel de coordonnées tkinter, message #2527582023-11-06T19:31:48+01:00phi/@phihttps://zestedesavoir.com/forums/sujet/17200/python-appel-de-coordonnees-tkinter/?page=1#p252758<p>Bonsoir, je travaille actuellement sur un projet de terminal en NSI où je dois créer un jeu vidéo à l’aide de tkinter cependant je rencontre des progblèmes dans mes classes déplacements, personnages et ataquer. Lorsque mes personnages se déplacent sur le canvas, ils peuvent s’attaquer si leur distance est inférieure à 20. Néanmoins une ereur se lève lorsque j’appie sur les touches d’attaque :
x1, _ = self.canvas.coords(self.character1)
ValueError: not enough values to unpack (expected 2, got 0). Je n’arrive pas à relier mes personnages créer avec ma classes personnages à leurs images possédant les coordonner dans la classe déplacement pour que ces dernières soit utiliser dans la classe attaquer. Commment dois-je m’y prendre?</p>Python manipulation d'image tkinter, message #2526002023-10-25T09:41:14+02:00entwanne/@entwannehttps://zestedesavoir.com/forums/sujet/17169/python-manipulation-dimage-tkinter/?page=1#p252600<p>Merci pas de soucis, bonne journée et n’hésite pas à ouvrir un nouveau sujet si tu rencontres d’autres problèmes !</p>Python manipulation d'image tkinter, message #2525912023-10-24T19:09:07+02:00ajarry/@ajarryhttps://zestedesavoir.com/forums/sujet/17169/python-manipulation-dimage-tkinter/?page=1#p252591<p>Bonjour, effectivement sans ça j’aurai un peu de mal pour afficher ce que je veux <img src="/static/smileys/svg/unsure.svg" alt=":euh:" class="smiley">
Du coup tout marche comme je veux (pour le moment), merci beaucoup pour ta précieuse aide !
Bonne soirée à toi.</p>Python manipulation d'image tkinter, message #2525612023-10-22T10:03:10+02:00entwanne/@entwannehttps://zestedesavoir.com/forums/sujet/17169/python-manipulation-dimage-tkinter/?page=1#p252561<p>Je ne vois rien dans la fonction <code>take_a_photo</code> qui permette de charger la nouvelle image.
Tu appelles <code>photo = ImageTk.PhotoImage(maphoto)</code> mais <code>maphoto</code> n’a pas changé, c’est toujours l’image définie ligne 90.</p>Python manipulation d'image tkinter, message #2525392023-10-20T14:09:59+02:00ajarry/@ajarryhttps://zestedesavoir.com/forums/sujet/17169/python-manipulation-dimage-tkinter/?page=1#p252539<p>Bien sûr, le voici :</p>
<div class="hljs-code-div hljs-code-text"><div class="hljs-line-numbers"><span data-count="1"></span><span data-count="2"></span><span data-count="3"></span><span data-count="4"></span><span data-count="5"></span><span data-count="6"></span><span data-count="7"></span><span data-count="8"></span><span data-count="9"></span><span data-count="10"></span><span data-count="11"></span><span data-count="12"></span><span data-count="13"></span><span data-count="14"></span><span data-count="15"></span><span data-count="16"></span><span data-count="17"></span><span data-count="18"></span><span data-count="19"></span><span data-count="20"></span><span data-count="21"></span><span data-count="22"></span><span data-count="23"></span><span data-count="24"></span><span data-count="25"></span><span data-count="26"></span><span data-count="27"></span><span data-count="28"></span><span data-count="29"></span><span data-count="30"></span><span data-count="31"></span><span data-count="32"></span><span data-count="33"></span><span data-count="34"></span><span data-count="35"></span><span data-count="36"></span><span data-count="37"></span><span data-count="38"></span><span data-count="39"></span><span data-count="40"></span><span data-count="41"></span><span data-count="42"></span><span data-count="43"></span><span data-count="44"></span><span data-count="45"></span><span data-count="46"></span><span data-count="47"></span><span data-count="48"></span><span data-count="49"></span><span data-count="50"></span><span data-count="51"></span><span data-count="52"></span><span data-count="53"></span><span data-count="54"></span><span data-count="55"></span><span data-count="56"></span><span data-count="57"></span><span data-count="58"></span><span data-count="59"></span><span data-count="60"></span><span data-count="61"></span><span data-count="62"></span><span data-count="63"></span><span data-count="64"></span><span data-count="65"></span><span data-count="66"></span><span data-count="67"></span><span data-count="68"></span><span data-count="69"></span><span data-count="70"></span><span data-count="71"></span><span data-count="72"></span><span data-count="73"></span><span data-count="74"></span><span data-count="75"></span><span data-count="76"></span><span data-count="77"></span><span data-count="78"></span><span data-count="79"></span><span data-count="80"></span><span data-count="81"></span><span data-count="82"></span><span data-count="83"></span><span data-count="84"></span><span data-count="85"></span><span data-count="86"></span><span data-count="87"></span><span data-count="88"></span><span data-count="89"></span><span data-count="90"></span><span data-count="91"></span><span data-count="92"></span><span data-count="93"></span><span data-count="94"></span><span data-count="95"></span><span data-count="96"></span><span data-count="97"></span><span data-count="98"></span><span data-count="99"></span><span data-count="100"></span><span data-count="101"></span><span data-count="102"></span><span data-count="103"></span><span data-count="104"></span><span data-count="105"></span><span data-count="106"></span><span data-count="107"></span><span data-count="108"></span><span data-count="109"></span><span data-count="110"></span><span data-count="111"></span><span data-count="112"></span><span data-count="113"></span><span data-count="114"></span><span data-count="115"></span><span data-count="116"></span></div><pre><code class="hljs language-text">from tkinter import *
from picamera import PiCamera
import time
from time import sleep
from PIL import Image, ImageTk
import os
from pynput import keyboard
camera = PiCamera()
###chemin d'accès photos
path = '/home/pi/Desktop/Python/Projet/Photos/Photo'
###décompte en secondes
count = 2
###personnalisation de la camera
camera.resolution = (1920,1080)
camera.framerate = 60
###fonction camera
def take_a_photo():
###date/heure (locale) de la photo
dateiso = time.strftime("%Y-%m-%d_%H-%M-%S")
camera.start_preview(fullscreen = False, window = (50,50,640,480))
countdown(count)
camera.capture(path+'_'+dateiso+'.jpeg')
photo_name = (path+'_'+dateiso+'.jpeg')
count_label.config(text='Photographie effectuée')
camera.stop_preview()
#####maphoto = Image.open(path+'_'+dateiso+'.jpeg') ## Chargement d'une image à partir de PIL
#####photo = ImageTk.PhotoImage(maphoto)
#####photo_label = Label(frame, image=photo)
#####photo_label.image = photo
print (photo_name)
#photo = Image.open(photo_name)
#photo.show(photo_label)
photo = ImageTk.PhotoImage(maphoto)
photo_label.config(image = photo)
photo_label.image = photo
###fonction horloge
def digital_clock():
time_live = time.strftime("%H:%M:%S")
horloge.config(text=time_live)
###actualisation toutes les 500ms
horloge.after(500, digital_clock)
###fonction décompte
def countdown(count):
while count:
mins, secs = divmod(count, 60)
timer = '{:d}'.format(secs)
#count_label.config(text=count)
print(timer, end="\r")
time.sleep(1)
count -= 1
print('Photographie effectuée')
###creation mainWindow
mainWindow = Tk()
###personnalisation de la fenetre
mainWindow.title("Instant Photo")
mainWindow.config(background='black')
mainWindow.attributes('-fullscreen', True) #application en plein écran
mainWindow.bind('<F>',lambda e: mainWindow.destroy()) #pour enlever le plein écran et quitter
###creation frame
frame = Frame(mainWindow, width=350, height=200, bg='black')
###ajout texte
label_title = Label(mainWindow, text="Instant Photo", font=("koliko-Bold.ttf", 60), bg='black',fg='white')
###ajout décompte
count_label = Label(frame, font=("koliko-Bold.ttf", 20), bg='black', fg='white')
###ajout bouton photo
photo_button = Button(frame, text="Prendre une photo", font=("koliko-Bold.ttf", 20), bg='white', fg='black', command=take_a_photo)
###ajout commentaires
commentaries_label = Label(frame, text="Merci de rester immobile pendant la prise", font=("koliko-Bold.ttf", 10), bg='black', fg='white')
###photo
maphoto = Image.open("imagetest.png") ## Chargement d'une image à partir de PIL
maphoto = maphoto.resize((1920,1080))
photo = ImageTk.PhotoImage(maphoto) ## Création d'une image compatible Tkinter
photo_label = Label(frame, image=photo, width = 1920, height = 1080)
photo_label.image = photo # Maintient en vie de photo dans un objet non détruit
###ajout horloge
horloge = Label(mainWindow, font="koliko-Bold.ttf", bg='black', fg='white')
""" +CREATION+ """
""" -CREATION- """
""" +PLACEMENT+ """
label_title.pack(pady=(50,0))
horloge.pack()
frame.pack(expand=True)
photo_label.grid(row=0, column=0)
count_label.grid(row=1, column=0)
photo_button.grid(row=2, column=0)
commentaries_label.grid(row=3, column=0)
""" -PLACEMENT- """
###appel de la fonction horloge
digital_clock()
###affichage
mainWindow.mainloop()
</code></pre></div>Python manipulation d'image tkinter, message #2525032023-10-18T15:53:14+02:00entwanne/@entwannehttps://zestedesavoir.com/forums/sujet/17169/python-manipulation-dimage-tkinter/?page=1#p252503<p>Est-ce que tu peux redonner le code complet actuel de ton application ?</p>Python manipulation d'image tkinter, message #2525002023-10-18T12:02:26+02:00ajarry/@ajarryhttps://zestedesavoir.com/forums/sujet/17169/python-manipulation-dimage-tkinter/?page=1#p252500<p>Je l’ai placé après la ligne <code>photo_label.config(image=photo)</code> mais cela ne fonctionne pas.</p>Python manipulation d'image tkinter, message #2524992023-10-18T11:47:50+02:00entwanne/@entwannehttps://zestedesavoir.com/forums/sujet/17169/python-manipulation-dimage-tkinter/?page=1#p252499<p>Le <code>photolabel.image = photo</code> devrait faire l’affaire.</p>Python manipulation d'image tkinter, message #2524982023-10-18T11:41:52+02:00ajarry/@ajarryhttps://zestedesavoir.com/forums/sujet/17169/python-manipulation-dimage-tkinter/?page=1#p252498<p>D’accord je comprends mais du coup, comment faire pour que je puisse changer la photo (en la sauvegardant dans la frame) depuis ma fonction ?</p>Python manipulation d'image tkinter, message #2524922023-10-17T19:06:35+02:00entwanne/@entwannehttps://zestedesavoir.com/forums/sujet/17169/python-manipulation-dimage-tkinter/?page=1#p252492<p>Ah, je pense que le soucis est lié à la perte de référence vers ton image. C’est le même soucis contre lequel tu essayais de lutter dans ton code initial en faisant</p>
<div class="hljs-code-div hljs-code-python"><pre><code class="hljs language-python">photolabel.image = photo <span class="hljs-comment"># Maintient en vie de photo dans un objet non détruit</span>
</code></pre></div>
<p>En effet, tkinter intègre l’image dans le label mais n’en garde pas la trace.
Ce qui fait qu’à la sortie de la fonction, l’image n’est plus référencée par aucune variable (les variables de la fonction étant détruites) et n’existe donc plus : elle disparaît (la frame grise).</p>
<p>La solution que tu avais consiste donc à garder la trace de cette image, en l’enregistrant comme un attribut d’un objet défini au niveau du module (<code>photolabel</code> ici).<br>
Une autre solution pourrait être de stocker les images chargées dans une liste ou un dictionnaire défini dans le module.</p>Python manipulation d'image tkinter, message #2524892023-10-17T15:55:36+02:00ajarry/@ajarryhttps://zestedesavoir.com/forums/sujet/17169/python-manipulation-dimage-tkinter/?page=1#p252489<p>J’ai l’impression que cela me remplace bien la photo présente à la base seulement elle est rempacée par une frame totalement grise (pas par la photo que je viens de prendre.</p>Python manipulation d'image tkinter, message #2524882023-10-17T15:16:18+02:00entwanne/@entwannehttps://zestedesavoir.com/forums/sujet/17169/python-manipulation-dimage-tkinter/?page=1#p252488<p>Oui c’est possible sans problème, l’erreur rencontrée ne vient pas de là.</p>
<p>L’erreur est que dans ta fonction tu dois avoir cette ligne <code>photo_label.config(image=photo)</code> juste avant une ligne qui définit cette variable <code>photo</code> (<code>photo = ...</code>).<br>
Python ne sait alors pas si le nom <code>photo</code> fait référence à une variable globale définie dans le module (la variable <code>photo</code> dans ton code initial) ou à une variable locale à ta fonction mais qui n’est pas encore définie.</p>
<p>La solution serait donc de placer la définition de la variable avant la ligne qui l’utilise.</p>Python manipulation d'image tkinter, message #2524852023-10-17T13:45:41+02:00ajarry/@ajarryhttps://zestedesavoir.com/forums/sujet/17169/python-manipulation-dimage-tkinter/?page=1#p252485<p>Avec simplement la ligne photo_label.config(image=photo), il y a une erreur de variable : local variable 'photo' referenced before assigment.</p>
<p>Ai-je le droit de modifier un label depuis une fonction externe ?</p>Python manipulation d'image tkinter, message #2524832023-10-17T12:29:59+02:00entwanne/@entwannehttps://zestedesavoir.com/forums/sujet/17169/python-manipulation-dimage-tkinter/?page=1#p252483<p>Bonjour, pour mettre à jour l’image un label tu peux utiliser <code>photolabel.config(image=photo)</code> avec <code>photo</code> ton nouvel objet <code>ImageTk</code>.</p>Python manipulation d'image tkinter, message #2524822023-10-17T12:01:31+02:00ajarry/@ajarryhttps://zestedesavoir.com/forums/sujet/17169/python-manipulation-dimage-tkinter/?page=1#p252482<p>Bonjour,
désolé pour la réponse tardive …</p>
<p>Mon problème actuel à bien été résolu je te remercie !
J’ai une autre question maintenant <img src="/static/smileys/svg/clin.svg" alt=";)" class="smiley"> … Comment faire pour que lorsque je prends une nouvelle photo, la photo initiale soit remplacée ?
Le problème (je pense que la compréhension de mon côté n’est pas bonne) est que la prise d’une nouvelle photo est faite à l’aide d’une fonction et que ma photo (à la base) est dans une variable de la fonction principale.</p>
<p>Merci beaucoup pour l’aide !</p>
<p>PS : le code n’a pas changé, c’est le même que lorsque j’ai ouvert cette discussion.</p>Python manipulation d'image tkinter, message #2524482023-10-12T15:20:45+02:00entwanne/@entwannehttps://zestedesavoir.com/forums/sujet/17169/python-manipulation-dimage-tkinter/?page=1#p252448<p>Bonjour,</p>
<p>Je remarque dans ton code une certaine confusion dans les variables : <code>photolabel</code> est définie ligne 88 mais n’est pas utilisée et <code>photo_label</code> est utilisée ligne 101 mais n’est définie qu’à l’intérieur d’une fonction (donc n’existe pas à la racine du programme).</p>
<p>Mais en simplifiant le code à l’extrême pour ne garder que ce qui pose problème ici, on obtient :</p>
<div class="hljs-code-div hljs-code-python"><div class="hljs-line-numbers"><span data-count="1"></span><span data-count="2"></span><span data-count="3"></span><span data-count="4"></span><span data-count="5"></span><span data-count="6"></span><span data-count="7"></span><span data-count="8"></span><span data-count="9"></span><span data-count="10"></span><span data-count="11"></span><span data-count="12"></span><span data-count="13"></span><span data-count="14"></span><span data-count="15"></span></div><pre><code class="hljs language-python"><span class="hljs-keyword">from</span> tkinter <span class="hljs-keyword">import</span> *
<span class="hljs-keyword">from</span> PIL <span class="hljs-keyword">import</span> Image, ImageTk
mainWindow = Tk()
mainWindow.title(<span class="hljs-string">"PhotoBox"</span>)
mainWindow.attributes(<span class="hljs-string">'-fullscreen'</span>, <span class="hljs-literal">True</span>)
maphoto = Image.<span class="hljs-built_in">open</span>(<span class="hljs-string">"image.jpg"</span>)
photo = ImageTk.PhotoImage(maphoto)
photolabel = Label(mainWindow, image=photo, width=<span class="hljs-number">500</span>, height=<span class="hljs-number">400</span>)
photolabel.pack()
mainWindow.mainloop()
</code></pre></div>
<p>qui définit simplement une image (avec une taille de 500×400) et l’affiche dans une fenêtre.
La photo que j’utilise de mon côté a une définition de 1391×927, et se retrouve effectivement rognée puisque seul le coin haut-gauche tient dans la zone de 500×400.</p>
<p>Une solution serait de redimensionner la photo côté PIL (l’objet obtenu par <code>Image.open</code>) pour qu’elle tienne dans la zone définie.
Ainsi en ajoutant <code>maphoto.thumbnail((500, 400))</code> entre mes lignes 9 et 10 j’obtiens le comportement voulu : la photo est redimensionnée en fonction de la taille donnée, tout en conservant son ratio (environ 1,5 et non 1,25 comme défini par la zone 500×400).</p>
<p>Pour redimensionner sans conserver le ratio, tu pourrais utiliser <code>maphoto = maphoto.resize((500, 400))</code>.</p>