Wouter
Wouter

Reputation: 173

Assign command to button - Tkinter

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?

Upvotes: 2

Views: 1531

Answers (1)

Wouter
Wouter

Reputation: 173

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.

Upvotes: 2

Related Questions