View Single Post
Old 02-12-2025, 04:12 AM   #1431
capink
Wizard
capink ought to be getting tired of karma fortunes by now.capink ought to be getting tired of karma fortunes by now.capink ought to be getting tired of karma fortunes by now.capink ought to be getting tired of karma fortunes by now.capink ought to be getting tired of karma fortunes by now.capink ought to be getting tired of karma fortunes by now.capink ought to be getting tired of karma fortunes by now.capink ought to be getting tired of karma fortunes by now.capink ought to be getting tired of karma fortunes by now.capink ought to be getting tired of karma fortunes by now.capink ought to be getting tired of karma fortunes by now.
 
Posts: 1,212
Karma: 1995558
Join Date: Aug 2015
Device: Kindle
Quote:
Originally Posted by Nicolas.Laurent View Post
Hello everyone

I'm back with a new problem.

I wrote this template for the Single Field Edit module, the idea being to copy author names into a dialog box and format them before assigning them to the authors field.
Everything works perfectly, except that my dialog box opens twice, and also opens when I save the code. It works, so it's not too bad, but a bit annoying.
What should I do to solve the problem?

And once again, thank you so much for this incredible plugin, which has saved me a lot of time!

Code:
Code:
python:

def evaluate(book, context):

	from PyQt5.QtWidgets import QInputDialog

	sortie = ""
	nom = ", ".join(book.authors)

	source, ok = QInputDialog.getText(None, "Modification des auteurs", "Entrez les auteurs :")

	if not ok:
	        return nom

	source = source.replace(" (Auteur)", "").replace(" & ", ", ").replace(" et ", ", ").strip()
    
	nom_transforme = []
	for auteur in source.split(", "):  
		parts = auteur.split(" ", 1)
		if len(parts) == 2:
			prenom, nom_famille = parts
			nom_transforme.append(f"{nom_famille.upper()}, {prenom.title()}")
		else:
			nom_transforme.append(auteur)

	return " & ".join(nom_transforme)
Don't put GUI code in templates. Put it in a module and pass the result as a chain variable as discussed previously.
capink is online now   Reply With Quote