2
votes

I want to find out which command I have to assign to the button in my Tkinter GUI in order to print the results.

The setup is to run aaaa.py.

def question():
    import xxxx
    return xxxx.hoeveel()


if __name__ == '__main__':
    from bbbb import response
    def juist():
        return response()
    print juist()

When running aaaa.py I get a Tkinter GUI based on script xxxx.py

from Tkinter import *
import ttk



def hoeveel():
    return int(x=gewicht.get(), base=10)

frame = Tk()

gewicht = StringVar()

a = Label(frame, text="Wat is uw gewicht in kilogram?:").grid(row=1, column=1, sticky='w')
aa = Entry(frame, text="value", textvariable=gewicht, justify='center', width=10)
aa.grid(row=1, column=2, padx=15)

bereken = ttk.Button(frame, text='Bereken')
bereken.grid(column=1, row=2, columnspan=2, ipadx=15, pady=25)
mainloop()

The input given in the Tkinter GUI xxxx.py is sent to bbbb.py for some calculations.

from aaaa import question
mass_stone = question() * 2.2 / 14


def response():
    return str("Uw gewicht in kilograms is gelijk aan " + ("{0:.5}".format(mass_stone)) + " stone.")

My issue is that I only get the output "Uw gewicht in kilograms is gelijk aan "x (depending on the value input)" stone, when I close the Tkinter window.

I want to get the results when I press the button.

Any tips?

1
What results? Get them when and do what with them? - martineau
@martineau, when you run aaaa.py you get a Tkinter window. Inside that window you get a question "Wat is uw gewicht in kilogram?" Next to the question you get an Entry box. In that Entry box you fill in your weight in kg. when you close the Tkinter window now with the "X", in CMD you will get the answer based on the calculations in bbbb.py, which is "Uw gewicht in kilograms is gelijk aan 15.714 stone." What I would like to achieve is than instead of closing the Tkinter window to get the result that when you press the "Berekening" button that the result gets printed. - Wouter

1 Answers

2
votes

I think I've found an answer, but I'm not sure it's the most elegant way to do it. If you know a different way and more correct way please let met know.

So, you need to run aaaa.py

import xxxx
def question():
    return xxxx.hoeveel()

xxxx.py

from Tkinter import *
import ttk
import bbbb

def hoeveel():
    return int(x=gewicht.get(), base=10)

frame = Tk()

gewicht = StringVar()

a = Label(frame, text="Wat is uw gewicht in kilogram?:").grid(row=1, column=1, sticky='w')
aa = Entry(frame, text="value", textvariable=gewicht, justify='center', width=10)
aa.grid(row=1, column=2, padx=15)

bereken = ttk.Button(frame, text='Bereken', command=bbbb.berekening)
bereken.grid(column=1, row=2, columnspan=2, ipadx=15, pady=25)
mainloop()

and finally

bbbb.py

from aaaa import question
def berekening():
    mass_stone = question() * 2.2 / 14
    print mass_stone

When you run aaaa.py you get the Tkinter Gui with the question. Fill in your weight, and press "Bereken" You get the answer printed like I wanted.