First off, I am new to Python and new to programming. but I am trying to make a program that puts together a database with user input. In this particular example, I am trying to take a date from user input and add it to a dataclass
here is the relevant code.
import tkinter as tk
from tkinter import ttk
import tkcalendar
from tkcalendar import Calendar, DateEntry
window = tk.Tk()
window.title("Route Cards")
def enter_data():
date = date_entry.get()
print(date)
def date_entry ():
def print_sel():
print(cal.selection_get())
top = tk.Toplevel(window)
cal = Calendar(top, font="Arial 14", selectmode='day', locale='en_US',
cursor="hand1", year=2024, month=1, day=26)
cal.pack(fill="both", expand=True)
ttk.Button(top, text="ok", command=print_sel).pack()
date = ttk.Button(customer_info_frame, text='date', command=cal_select)
date.grid(row=7, column=1)
window.mainloop()
the date box opens a window and when the user selects the date and chooses ok it prints. but I want to take that date and add it to the function enter_data so I can add it to a dataclass as a str (or a datetime)
but all I can do is print it in the terminal. I have tried Return but it just returns "None"
The answer to this particular problem would be helpful but i just need help in general on how to get a user input() and put that information elsewhere. Thank you in advance!
EDIT: I got this particular code to work with quite a bit of re writing, but the main question stands. Maybe the problem I am having is with taking input from a function and using it? Like I said, I am new, so I'm not really sure what the problem is. But is there a way to take a string from input from one function and use it in another function?
In your case, the most common solutions are: passing the values of local variables using a lambda or using a global textvariable.
See also: How to pass arguments to a Button command in Tkinter
Using a lambda to get a value from an input field.
Using a textvariable to get or set the value of an input field.