I have this 'simple' code
import tkinter as tk
from tkinter import ttk
import datetime
class Page(tk.Frame):
def __init__(self, root):
# Set up
super().__init__(root)
# Labels
self.labels = {}
# Buttons
self.buttons = {}
# Entries
self.entries = {}
# Year label
self.labels["year"] = ttk.Label(self, text="Year")
self.labels["year"].grid(row=0, column=0)
# Month label
self.labels["month"] = ttk.Label(self, text="Month")
self.labels["month"].grid(row=0, column=1)
# Day label
self.labels["day"] = ttk.Label(self, text="Day")
self.labels["day"].grid(row=0, column=2)
# Year entry
self.year = tk.IntVar()
self.entries["year"] = ttk.Entry(self, textvariable=self.year)
self.entries["year"].delete(0, "end")
self.entries["year"].grid(row=1, column=0)
# Month entry
self.month = tk.IntVar()
self.entries["month"] = ttk.Entry(self, textvariable=self.month)
self.entries["month"].delete(0, "end")
self.entries["month"].grid(row=1, column=1)
# Day entry
self.day = tk.IntVar()
self.entries["day"] = ttk.Entry(self, textvariable=self.day)
self.entries["day"].delete(0, "end")
self.entries["day"].grid(row=1, column=2)
# Today button
self.buttons["today"] = ttk.Button(self, text="Today",
command=lambda:self._f_buttonToday())
self.buttons["today"].grid(row=2, column=0, columnspan=3, sticky="WE")
def _f_buttonToday(self):
year, month, day = map(int, datetime.date.today().isoformat().split("-"))
self.year.set(year)
self.month.set(month)
self.day.set(day)
#self.entries["year"].delete(0, "end")
self.entries["year"].insert(0, self.year.get())
self.entries["month"].insert(0, self.month.get())
self.entries["day"].insert(0, self.day.get())
root = tk.Tk()
page = Page(root)
page.pack()
root.mainloop()
When you press the button, the text in the entries is updated, but it is doubled:
I've already checked and the self.year
, self.month
and self.day
variables are right and the button command is executed only once.
How can I solve this? I don't know if it might be related or not, but if I uncomment self.entries["year"].delete(0, "end")
, I get the error:
Exception in Tkinter callback
Traceback (most recent call last):
File "C:\Users\acgc9\AppData\Local\Programs\Python\Python311\Lib\tkinter\__init__.py", line 592, in get
return self._tk.getint(value)
^^^^^^^^^^^^^^^^^^^^^^
_tkinter.TclError: expected integer but got ""
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:\Users\acgc9\AppData\Local\Programs\Python\Python311\Lib\tkinter\__init__.py", line 1948, in __call__
return self.func(*args)
^^^^^^^^^^^^^^^^
File "D:\Sync1\Code\Python3\SportsPy\here\test.py", line 45, in <lambda>
command=lambda:self._f_buttonToday())
^^^^^^^^^^^^^^^^^^^^^
File "D:\Sync1\Code\Python3\SportsPy\here\test.py", line 56, in _f_buttonToday
self.entries["year"].insert(0, self.year.get())
^^^^^^^^^^^^^^^
File "C:\Users\acgc9\AppData\Local\Programs\Python\Python311\Lib\tkinter\__init__.py", line 594, in get
return int(self._tk.getdouble(value))
^^^^^^^^^^^^^^^^^^^^^^^^^
_tkinter.TclError: expected floating-point number but got ""
Thanks!
CodePudding user response:
So I found the issue:
def _f_buttonToday(self):
year, month, day = map(int, datetime.date.today().isoformat().split("-"))
self.year.set(year)
self.month.set(month)
self.day.set(day)
#self.entries["year"].delete(0, "end")
self.entries["year"].insert(0, self.year.get())
self.entries["month"].insert(0, self.month.get())
self.entries["day"].insert(0, self.day.get())
When updating self.year
, the self.entries["year"]
content is already updated.
For the tkinter.Entry.delete
issue, I just moved that lines.
Right function:
def _f_buttonToday(self):
year, month, day = map(int, datetime.date.today().isoformat().split("-"))
self.entries["year"].delete(0, "end")
self.entries["month"].delete(0, "end")
self.entries["day"].delete(0, "end")
self.year.set(year)
self.month.set(month)
self.day.set(day)