2

I want to create a button in tkinter and when pressed, have that button call a different program into this program. (this is just an example) Say I have the one program that shows the button that says "Square root" and I have another program that takes a number such as: 4, using the input() function, and finds the square root of 4. I want that button when pressed to call the "square root program" and input it into the "button" program. (Again. just an example. if I really wanted to do the square root stuff I would just make a different function called: def square_root())

#Button.py
from tkinter import *
root = Tk()

def call_the_other_program():
    #This is the part I don't know how to do...
    #Do I import the different program somehow?
    #Is this even possible xD
b = Button(root, text = "Square root", command = call_the_other_program())

Square root program:

import math
x = input("input a number")
print("the square root of", x, "is:", math.sqrt(x))
#I want this to be the program that is called

Thanks for explaining to me! 😊

0

1 Answer 1

1

You can define a function square_root in the other_prog.py, import it and call it.

I would suggest that you keep all the related files in the same folder to start with, so as not to complicate the import process.

other_prog.py

import math

def square_root():
    x = float(input('enter a square'))
    return math.sqrt(x)

GUI.py

import tkinter as tk
from other_prog import square_root


def call_square_root():
    print(f'the square root is {square_root()}')


root = Tk()
b = Button(root, text = "Square root", command=call_square_root())
b.pack()

root.mainloop()

The example is a little bit contrived; the button command could directly be square_root, as it is in the NameSpace after import.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.