'tkinter form: can't get reentered value from entry widget
import tkinter as tk
import tkmacosx as tkm
class Journal():
def __init__(self, parent, report_week=None):
root = self.root = tk.Toplevel(parent)
main_frame = tkm.SFrame(root)
main_frame.pack(expand=1, fill='both')
report_week_frame = tk.Frame(main_frame)
report_week_frame.pack(padx=8, pady=8, fill='x')
report_week_frame.columnconfigure(0, weight=1) # expand column 0
report_week_label = tk.Label(report_week_frame, text="Report week")
report_week_label.grid(row=2, column=0, sticky='w')
textEntry_report_week = tk.StringVar()
textEntry_report_week.set(report_week)
report_week_entry_box = tk.Entry(report_week_frame, width=40, textvariable=textEntry_report_week)
report_week_entry_box.grid(row=2, column=2, sticky='e')
button_frame = tk.Frame(main_frame)
button_frame.pack(pady=20)
OK_button = tkm.Button(button_frame, text="OK", padx=5, command=self.root.quit)
OK_button.grid(row=0, column=0)
self.output = f"Here I want the value entered by the user in the textbox. Not the value that is set: {textEntry_report_week.get()}"
class MainApp(tk.Frame):
def __init__(self, parent, *args, **kwargs):
tk.Frame.__init__(self, parent, *args, **kwargs)
...
self.prepareReportButton = tkm.Button(self.report_buttons_frame, text="report")
self.prepareReportButton.bind("<Button-1>", self.week_or_month)
self.prepareReportButton.grid(row=0, column=0, padx=10)
def week_or_month(self, event):
reportWeek = self.get_report_week() # gets a number, e.g. 5 from the web-scraping
journal = Journal(self.parent, report_week=reportWeek)
journal.root.mainloop()
journal.root.destroy()
print("The journal output is: ", journal.output)
def main():
root = tk.Tk()
app = MainApp(root)
app.pack()
root.mainloop()
if __name__ == "__main__":
main()
I have a web-scraper script that gets the report week number from a site. E.g. the number 5. When I press the prepareReportButton in the MainApp a form opens. In this form there is an entry textbox. This box's value is set to the web-scraped data, i.d. 5. The user can however enter another number in this entry box.
When I press the OK-button on the form, the form closes and the report week number is printed to the terminal (journal.output). But it only prints the predefined or set number, in this case 5. If I change the number in the form that pops up, to e.g. 7, this number is not printed to the terminal. Why is not the number entered in the entry box, printed (in this example the number 7)? I.e why is not the value updated if I enter my own value in the entry box?
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
