Get input from tkinter and then close the window

I have two python files, one that stores the inputData.py code and the one that is the main main_project.py file.

In inputData, I have this code:

class Prompt(tk.Tk):
    def __init__(self):
        tk.Tk.__init__(self)
        self.entry = tk.Entry(self)
        self.button = tk.Button(self, text="Get", command=self.on_button)
        self.button.pack()
        self.entry.pack()

    def on_button(self):
        self.inputDt = self.entry.get()

      

In main_project I have this code:

from inputData import Prompt
promptData = Prompt()
promptData.mainloop()

class Hearing(object):
    numberBirths = promptData.inputDt

      

What I'm trying to do is assign the value of numberBirths to the value from the input in tkinter, and after that I need a prompt to close and continue with the rest of the code. Can you help me?

+3


source to share


2 answers


Your entry widget does not have a text variable for using get and set function
add the following elements before entry and edit entry () by inserting textvariable



input_var=0
self.entry = tk.Entry(self,textvariable=input_var)

      

0


source


You can use methods .quit()

and .destroy()

:

inputData.py

:

import tkinter as tk

class Prompt(tk.Tk):
    def __init__(self):
        self.answer = None
        tk.Tk.__init__(self)
        self.entry = tk.Entry(self)
        self.button = tk.Button(self, text="Get", command=self.on_button)
        self.button.pack()
        self.entry.pack()

    def on_button(self):
        self.answer = self.entry.get()
        self.quit()

      



main_project.py

:

from inputData import Prompt

class Hearing(object):
    def __init__(self):
        promptData = Prompt()
        promptData.mainloop()
        promptData.destroy()
        self.numberBirths = promptData.answer
        print("Births:", self.numberBirths)
        # do something else with self.numberBirths

Hearing()

      

+1


source







All Articles