0

On ticket.py module i have a Tkinter frame; value = Entry(frame), and on the same module I have a button where command=exoutput;

def exoutput(): print value.get()

I would like to import the value to othermodule.py on the button command/ when I hit the button. Currently when I import, the print is generated from the exoutput() function, rather than from the othermodule.py file.

Suggestions on how to print the value on othermodule.py?

# ticket.py
from Tkinter import*

window = Tk()
window.title("Entry")

frame = Frame(window)
value = Entry(frame)

def exoutput():
    print value.get()

btnStage = Button(frame, text='ACTION', command=exoutput)
btnStage.pack(side=RIGHT, padx=2)
value.pack(side=LEFT)
frame.pack(padx=10, pady=10)

window.resizable(0, 0)
window.mainloop()

The other file, I've tried something like this;

# othermodule.py
import ticket

usersinput = ticket.value.get()
print usersinput
Andrew Dunlop
  • 11
  • 1
  • 3
  • 1
    please provide a [mcve] – Bryan Oakley Jan 22 '18 at 15:04
  • I think you either need multi-threading, or swap your file contents. Nothing runs _after_ `mainloop` until the `Tk` instance is `destroy`ed. – Nae Jan 22 '18 at 17:25
  • Alternatively, you could instead structure your `ticket` py with OOP, and fetch GUI object from it by your `othermodule` to use it as you please. See https://stackoverflow.com/a/17470842/7032856. – Nae Jan 22 '18 at 19:36

1 Answers1

0

I think you either need multi-threading, or swap your file contents. Nothing runs after mainloop until the Tk instance is destroyed.

Alternatively, you could structure your ticket.py in OOP, and fetch GUI object from it by your othermodule to use it as you please. Below is an example:

ticket.py:

#import tkinter as tk
import Tkinter as tk

class Window(tk.Tk):
    def __init__(self, *args, **kwargs):
        tk.Tk.__init__(self, *args, **kwargs)
        self.title("Entry")
        self.resizable(False, False)

        # create widgets
        self.frame = tk.Frame(self)
        self.value = tk.Entry(self.frame)
        self.btn_stage = tk.Button(self.frame, text="ACTION")

        # widgets layout
        self.frame.pack(padx=10, pady=10)
        self.btn_stage.pack(side='right', padx=2)
        self.value.pack(side='left')

if __name__ == "__main__":
    root = Window()
    root.mainloop()

and othermodule.py:

import ticket


def put():
    global my_var_in_othermodule

    my_var_in_othermodule = ticket_GUI.value.get()
    ticket_GUI.destroy()


my_var_in_othermodule = ""

ticket_GUI = ticket.Window()
ticket_GUI.btn_stage['command'] = put
ticket_GUI.mainloop()

print(my_var_in_othermodule)

input()
Nae
  • 14,209
  • 7
  • 52
  • 79