Contrevien
Contrevien

Reputation: 124

Connecting the same GUI to two or more python files

i am building a small project which involves 4 python files which have their separate functionalities. There is however, a main.py file which uses all the others by importing them.

Now, i have to build a GUI for this project, which i am building within the main.py file. My problem is that some of the other files have functions which print on the console, when the whole project is run, i want those functions to print on the GUI instead. So, how do i print the text from the other file in a Text field created in the main file.

main.py

import second as s
from tkinter import *

def a():
    field.insert(END, "Hello this is main!")

root = Tk()
field = Text(root, width=70, height=5, bd=5, relief=FLAT)
button1 = Button(root, width=20, text='Button-1', command=a)
button2 = Button(root, width=20, text='Button-2', command=s.go)
root.mainloop()

second.py

def go():
    print("I want this to be printed on the GUI!")
#... and a bunch of other functions...

I just want that when user presses the button-2, then the function go() prints the text on the field

Upvotes: 0

Views: 1271

Answers (1)

wsy
wsy

Reputation: 140

I consider it the best way to try add field as an argument of function go.

Here is my code:

main.py

import second as s
from tkinter import *

def a(field):
    field.insert(END, "Hello this is main!\n")

root = Tk()

field = Text(root, width=70, height=5, bd=5, relief=FLAT)
button1 = Button(root, width=20, text='Button-1', command=lambda : a(field))
button2 = Button(root, width=20, text='Button-2', command=lambda : s.go(field))

#to display widgets with pack
field.pack()
button1.pack()
button2.pack()

root.mainloop()

second.py

from tkinter import *


def go(field):
    field.insert(END, "I want this to be printed on the GUI!\n")
    print("I want this to be printed on the GUI!")
    #something you want

The screenshot of effect of running:)

enter image description here

Upvotes: 1

Related Questions