ANN
ANN

Reputation: 63

Get an input from tkinter and then close the window

I have two python files, one in which i store the code inputData.py and one which is the main file, main_project.py.

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 am trying to do is to assign the value numberBirths the value from the input in tkinter, and after i do that, i need the prompt to close and continue with the rest of the code. Can you help me with that?

Upvotes: 3

Views: 2939

Answers (2)

TrakJohnson
TrakJohnson

Reputation: 2087

You can use the .quit() and .destroy() methods:

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()

Upvotes: 1

Tango
Tango

Reputation: 123

your entry widget does not have text variable for using get and set function
please add followings before entry and edit entry() by inserting textvariable

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

Upvotes: 0

Related Questions