3

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?

ANN
  • 63
  • 2
  • 6

2 Answers2

1

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()
TrakJohnson
  • 1,755
  • 2
  • 18
  • 31
  • 1
    Ehm, the `destroy` command should be included in the method `on_button`. Now it is placed after the `mainloop`, which means it'll never be evaluated because the script gets stuck in the mainloop. – arrethra Apr 03 '17 at 10:53
  • Really ? I tested it and it worked fine for me. EDIT: @arrethra Nevermind, you are right, I forgot I included a `.quit()` in my code but forgot to put it on the answer. – TrakJohnson Apr 03 '17 at 11:56
0

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)
Tango
  • 123
  • 2
  • 12
  • Thank you for your comment, i appreciate it. My problem is the code stops and doesn't continue to the next step... – ANN Apr 03 '17 at 09:22