0

I was working on a GUI app that I was hoping to use to run a python script inside of it(don't know if it's even possible) I was hoping to get a script to run in the GUI by importing it but I don't if this can work so I looked for another way and the only working thing I got too was copying my script and paste it in a function called script and then assign the start button with the command to run this function but this runs the script in the terminal when I press the start button and that is not what I want. I want my script to run inside the Frame I created, in other means print the text that is outputting in the terminal to the frame but I don't seem to get anywhere or find a solution

GUI:

import tkinter as tk
from tkinter import Button, Canvas, Image, PhotoImage, filedialog, Text
import os
from tkinter.constants import COMMAND, DISABLED

root = tk.Tk()

canvas = tk.Canvas(root, height = 750, width = 800, bg = "#110a60")
canvas.pack()

root.resizable(False, False)
 
frame = tk.Frame(root, bg = "black")
frame.place(relheight = 0.8, relwidth = 0.8, relx = 0.1, rely = 0.1)

start_button = Button(root, text = 'Start Program', padx = 10, pady = 5, fg = "#FFFF00", bg = "#012456", state = DISABLED)
start_button.place(x = 350, y = 680)

exit_button = Button(root, text = 'Exit', padx = 20, pady = 5, fg = "#FFFF00", bg = "#012456", command = exit)
exit_button.place(x = 368, y = 714.5 )

root.mainloop()

I have tried doing this:

import tkinter as tk
from tkinter import Button, Canvas, Image, PhotoImage, filedialog, Text
import os
from tkinter.constants import COMMAND, DISABLED

def script():
    #type my script here and use it as a command but it gets printed in my terminal, not in the canvas

root = tk.Tk()

canvas = tk.Canvas(root, height = 750, width = 800, bg = "#110a60")
canvas.pack()

root.resizable(False, False)

frame = tk.Frame(root, bg = "black")
frame.place(relheight = 0.8, relwidth = 0.8, relx = 0.1, rely = 0.1)

start_button = Button(root, text = 'Start Program', padx = 10, pady = 5, fg = "#FFFF00", bg = "#012456", command = script)
start_button.place(x = 350, y = 680)

exit_button = Button(root, text = 'Exit', padx = 20, pady = 5, fg = "#FFFF00", bg = "#012456", command = exit)
exit_button.place(x = 368, y = 714.5 )

root.mainloop()

But this only runs the script in the terminal, I want it to run inside the frame I made in other means I want the text outputting from the script to be printed inside the frame and every user input to be made inside the frame

Brief explanation:

I need the text that gets printed to my terminal to be printed inside the frame I made in my GUI

Thank you in advance.

SAM Acc
  • 38
  • 8
  • Probably, too vague of a question to say more – Sayse Jan 08 '22 at 22:30
  • What do you mean ? – SAM Acc Jan 08 '22 at 22:31
  • 1
    You can call a python function when you click a button. How that function is supposed to interact could be absolutely anything – Sayse Jan 08 '22 at 22:32
  • I mean like when I click the button it executes the script, is this possible? – SAM Acc Jan 08 '22 at 22:34
  • Yes, maybe. You already know you can import python code from another source into your python code because you’re doing it in your question already – Sayse Jan 08 '22 at 22:36
  • Yes I known I can I did it only if it was unclear what I was saying – SAM Acc Jan 08 '22 at 22:38
  • But what can I do with my script when I imported – SAM Acc Jan 08 '22 at 22:38
  • You could do something along the lines of what the `errorwindow3k.py` module does in [this answer](https://stackoverflow.com/a/49016673/355230) of mine does. – martineau Jan 08 '22 at 23:13
  • @martineau that's a lot of code that I can't understand I am not a python expert I am somewhat of descent but what I did understand is that I can import my script and use sys.stderr and sys.stdout to display the code in my GUI window? If yes how does it work – SAM Acc Jan 09 '22 at 15:48
  • You don't necessarily have to completely understand the `errorwindow3k` module's code to use it (as I did for years). What it does is pipe `stdout` and `stderr` to a separate tkinter-based application that does nothing but display the output sent to it in a window. – martineau Jan 09 '22 at 15:59
  • Actually `errorwindow3k` creates separate windows for `stdout` and `stderr`, depending on which of them has output written to it. I forgot to mention that to use it all you have to do is `import` it (as explained in the text of the linked answer). – martineau Jan 09 '22 at 16:17
  • can you make this more clear because I did embed my code into one single function called script and I went to my GUI code and I imported it (from program import script) but the problem is that it doesn't even run my GUI it runs my program in the terminal – SAM Acc Jan 09 '22 at 21:12
  • @martineau May I provide the script if it doesn't bother you so you can show me what you mean because its unclear how your way works – SAM Acc Jan 09 '22 at 21:13
  • No, sorry. All you need to do to use it is: Create a script that contains two lines: `import errorwindow3k`, followed by `print('hello world')`, then run it. – martineau Jan 09 '22 at 21:28

1 Answers1

1

Import your script (program.py in the same directory as the main file) in your header.

from program import function

then just call the function as tkinter button command

start_button = Button(root, text='Start Program', padx=10, pady=5, fg="#FFFF00",
                      bg="#012456", command=function)
martineau
  • 119,623
  • 25
  • 170
  • 301
Pepsi-Joe
  • 447
  • 1
  • 10