Je suis en train de reprendre mon code python avec tkinter afin d'utiliser la bibliothèque ttk pour un design plus sympa.
Mais je me heurte à un point que je ne n'arrive pas à comprendre. C'est un formulaire de saisie de "thème".
J'utilise un Entry (ligne 39) avec un textvariable (ligne 41)
Voici mon code :
def eTheme(*args):
e = textinput2.get()
print(e)
return(e)
# Main form for entering themes in the tb_theme database ---
def form_theme():
""" Formulaire principal de saisie des thèmes dans la base de données tb_theme """
# Global declaration in order to be able to use the variables outside the function
global tpl_theme, entry_theme, textinput2
# Style ttk pour des boutons plus design
style = ttk.Style()
# Style bouton
style.configure("BW.TButton", foreground = "black")
# Style label
style.configure("BW.TLabel", foreground='Black', background = 'Gray79')
# création de la fenêtre
tpl_theme = Toplevel()
tpl_theme.title(" Bienvenue dans la saisie des thèmes")
screen_x = int(tpl_theme.winfo_screenwidth())
screen_y = int(tpl_theme.winfo_screenheight())
tpl_theme_x = 400
tpl_theme_y = 125
pos_x = (screen_x // 2) - (tpl_theme_x // 2)
pos_y = (screen_y // 2) - (tpl_theme_y // 2)
geo = "{}x{}+{}+{}".format(tpl_theme_x, tpl_theme_y, pos_x, pos_y)
tpl_theme.geometry(geo)
tpl_theme.resizable(width=False, height=False)
tpl_theme.configure(bg='Gray79')
# Champs de saisie ---
theme_label = ttk.Label(tpl_theme, text="Thème", style = "BW.TLabel", font=("Arial", 11, "bold"))
theme_label.place(x=30, y=15)
# text variable
textinput2 = StringVar()
# Entry
entry_theme = ttk.Entry(tpl_theme, textvariable=textinput2, width="55")
entry_theme.place(x=30, y=37)
# ----
rec_btn = ttk.Button(tpl_theme, text="Enregistrer", style = "BW.TButton", command=lambda: rec_theme(eTheme.get('1.0', END)))
rec_btn.place(x=195, y=85)
Mais j'ai ce message d'erreur, alors que j'ai bien fait ma fonction en ligne 1 :
rec_btn = ttk.Button(tpl_theme, text="Enregistrer", style = "BW.TButton", command=lambda: rec_theme(eTheme.get('1.0', END)))
AttributeError: 'function' object has no attribute 'get'
- Edité par lagratteCchouette 2 décembre 2020 à 11:32:05
Vous êtes super, mais je ne comprends toujours pas.
J'ai un formulaire en Toplevel pour saisir des thèmes dans un base de données Sqlite3. Je vous mets le code dans son ensemble à la fin. J'ai un bouton rec_btn pour enregister ma saisie dans la BdD SQLITE 3.
rec_btn = ttk.Button(tpl_theme, text="Enregistrer", style = "BW.TButton", command=lambda: rec_theme(entry_theme.get("1.0", END)))
TypeError: get() takes 1 positional argument but 3 were given
Voici mon code dans son ensemble, car là je n'arrive pas à comprendre.
Pourriez m'expliquer comment je dois faire, s'il vous plaît ! Merci beaucoup par avance.
# Function for saving themes in the tb_theme database ---
def rec_theme(theme_theme):
""" Fonction d'enregistrement des thèmes dans la base de données tb_theme """
try:
connection = sqlite3.connect('mnesis.db') # Connection to the Database
cursor = connection.cursor() # cursor creation
new_theme = (cursor.lastrowid, theme_theme) # Retrieving button values
# les (?,?,?) represent the secure values which are entered in the intermediate variable
cursor.execute('INSERT INTO tb_theme VALUES(?,?)', new_theme)
except Exception as e:
print("ERREUR", e)
connection.rollback()
finally:
cursor.close()
connection.commit() # Validate the registration in the database
connection.close() # Closing the connection
def eTheme(*args):
global textinput2
e = textinput2.get()
print(e)
return(e)
# Main form for entering themes in the tb_theme database ---
def form_theme():
""" Formulaire principal de saisie des thèmes dans la base de données tb_theme """
# Global declaration in order to be able to use the variables outside the function
global tpl_theme, entry_theme, textinput2
# Style ttk pour des boutons plus design
style = ttk.Style()
# Style bouton
style.configure("BW.TButton", foreground = "black")
# Style label
style.configure("BW.TLabel", foreground='Black', background = 'Gray79')
# Style Entry
style.configure("EntryStyle.TEntry", Foreground="blue", background=[("active","red")], fieldbackground="red")
# création de la fenêtre
tpl_theme = Toplevel()
tpl_theme.title(" Bienvenue dans la saisie des thèmes")
screen_x = int(tpl_theme.winfo_screenwidth())
screen_y = int(tpl_theme.winfo_screenheight())
tpl_theme_x = 400
tpl_theme_y = 125
pos_x = (screen_x // 2) - (tpl_theme_x // 2)
pos_y = (screen_y // 2) - (tpl_theme_y // 2)
geo = "{}x{}+{}+{}".format(tpl_theme_x, tpl_theme_y, pos_x, pos_y)
tpl_theme.geometry(geo)
tpl_theme.resizable(width=False, height=False)
tpl_theme.configure(bg='Gray79')
# Champs de saisie ---
theme_label = ttk.Label(tpl_theme, text="Thème", style = "BW.TLabel", font=("Arial", 11, "bold"))
theme_label.place(x=30, y=15)
# text variable
textinput2 = StringVar()
# Entry
entry_theme = ttk.Entry(tpl_theme, textvariable=textinput2, width="55")
entry_theme.place(x=30, y=37)
# ----
rec_btn = ttk.Button(tpl_theme, text="Enregistrer", style = "BW.TButton", command=lambda: rec_theme(entry_theme.get("1.0", END)))
#rec_btn = ttk.Button(tpl_theme, text="Enregistrer", style="BW.TButton", command=lambda: rec_theme(eTheme))
rec_btn.place(x=195, y=85)
- Edité par lagratteCchouette 2 décembre 2020 à 17:32:36
Un grand très très grand merci à tous pour votre patience, c'était tout bête. Je finissais par me mélanger les yeux.
@loupsolitaire gracias
Voici mon code pour la communauté.
# Function for saving themes in the tb_theme database ---
def rec_theme(theme_theme):
""" Fonction d'enregistrement des thèmes dans la base de données tb_theme """
try:
connection = sqlite3.connect('mnesis.db') # Connection to the Database
cursor = connection.cursor() # cursor creation
new_theme = (cursor.lastrowid, theme_theme) # Retrieving button values
# les (?,?,?) represent the secure values which are entered in the intermediate variable
cursor.execute('INSERT INTO tb_theme VALUES(?,?)', new_theme)
except Exception as e:
print("ERREUR", e)
connection.rollback()
finally:
cursor.close()
connection.commit() # Validate the registration in the database
connection.close() # Closing the connection
# Delete Entry after recs
entry_theme.delete('0', END)
def eTheme(*args):
e = textinput2.get()
return(e)
# Main form for entering themes in the tb_theme database ---
def form_theme():
""" Formulaire principal de saisie des thèmes dans la base de données tb_theme """
# Global declaration in order to be able to use the variables outside the function
global tpl_theme, entry_theme, textinput2
# Style ttk pour des boutons plus design
style = ttk.Style()
# Style bouton
style.configure("BW.TButton", foreground = "black")
# Style label
style.configure("BW.TLabel", foreground='Black', background = 'Gray79')
# Style Entry
style.configure("EntryStyle.TEntry", Foreground="blue", background=[("active","red")], fieldbackground="red")
# création de la fenêtre
tpl_theme = Toplevel()
tpl_theme.title(" Bienvenue dans la saisie des thèmes")
screen_x = int(tpl_theme.winfo_screenwidth())
screen_y = int(tpl_theme.winfo_screenheight())
tpl_theme_x = 400
tpl_theme_y = 125
pos_x = (screen_x // 2) - (tpl_theme_x // 2)
pos_y = (screen_y // 2) - (tpl_theme_y // 2)
geo = "{}x{}+{}+{}".format(tpl_theme_x, tpl_theme_y, pos_x, pos_y)
tpl_theme.geometry(geo)
tpl_theme.resizable(width=False, height=False)
tpl_theme.configure(bg='Gray79')
# Champs de saisie ---
theme_label = ttk.Label(tpl_theme, text="Thème", style = "BW.TLabel", font=("Arial", 11, "bold"))
theme_label.place(x=30, y=15)
# text variable
textinput2 = StringVar()
# Entry
entry_theme = ttk.Entry(tpl_theme, textvariable=textinput2, style ="EntryStyle.TEntry", width="55")
entry_theme.place(x=30, y=37)
# ----
rec_btn = ttk.Button(tpl_theme, text="Enregistrer", style = "BW.TButton", command=lambda: rec_theme(entry_theme.get()))
rec_btn.place(x=195, y=85)
- Edité par lagratteCchouette 2 décembre 2020 à 19:57:00
Tkinter 'function' object has no attribute 'get'
× Après avoir cliqué sur "Répondre" vous serez invité à vous connecter pour que votre message soit publié.
× Attention, ce sujet est très ancien. Le déterrer n'est pas forcément approprié. Nous te conseillons de créer un nouveau sujet pour poser ta question.
Blond, bouclé, toujours le sourire aux lèvres...
Blond, bouclé, toujours le sourire aux lèvres...