Get variable from another file - python -
i creating tkinter program allows user enter text nice looking box rather python shell.
as use in multiple programs made function can used in other files.
i can run in file, not import variable here code.
file 1:
import tkinter tk def input_text(label_text, button_text): class sampleapp(tk.tk): def __init__(self): tk.tk.__init__(self) self.entry = tk.entry(self) self.button = tk.button(self, text=button_text, command=self.on_button) self.label = tk.label(self, text=label_text) self.label.pack(side = 'top', pady = 5) self.button.pack(side = 'bottom', pady = 5) self.entry.pack() def on_button(self): answer = self.entry.get() self.destroy() w = sampleapp() w.resizable(width=true, height=true) w.geometry('{}x{}'.format(180, 90)) w.mainloop() file 2:
import text_input ti text_input import answer ti.input_text('enter text', 'ok') i error importerror: cannot import name 'answer'
answer local variable withinbutton. if want toimport` it, need make package attribute:
import tkinter tk global answer def input_text(label_text, button_text): class sampleapp(tk.tk): ... def on_button(self): global answer answer = self.entry.get() however, very strange way access data. clean module design have object (sampleapp) @ hand, , extract answer method call app. more simply, why not return value on_button?
def on_button(self): answer = self.entry.get() self.destroy() return answer ... usage be
response = my_app.on_button()
Comments
Post a Comment