Connecting modules and packages

Asked

Viewed 63 times

1

I am trying to implement a GUI with python3x and Tkinter in separate scripts.

I have a gui_test folder organized as follows:

  • Gui (sub-folder): Unit.py and app.py (Tkinter)
  • utils (sub-folder): Unit.py and functions.py
  • Unit.py (file)
  • main.py (file)

Here’s what I wanna do:

1) the main.py calls the app.py

2) in the app.py has buttons and call functions in functions.py

3) I want functions.py to print the function output in the app.py GUI, but I’m not getting that connection.

The scripts are like this:

main.py:

from gui import app

if __name__ == '__main__':
    app.main()

app py.:

from tkinter import *
from utils import functions

class Application():
    def __init__(self, master=None):
        self.msg = Label(master, text= 'Primeira Mensagem')
        self.msg.pack()

        self.btnTrocaMsg = Button(master, text= 'Trocar', command= functions.trocaMsg)
        self.btnTrocaMsg.pack()

def main():
    root = Tk()
    Application(root)
    root.mainloop()

functions:

from gui import app

def trocaMsg():
    <caminho?>.msg['text'] = 'Mensagem trocada.'

Which way?

Files are properly grouped in folders?

I changed the app.py and functions.py:

app py.:

(...)
    self.btnTrocaMsg = Button(master, text= 'Trocar', command= ***self.troca***)
    self.btnTrocaMsg.pack()


***def troca(self):
    a = functions.trocaMsg(self)
    self.msg['text'] = a***
(...)

No functions.py:

from gui import app


    ***def trocaMsg(msg):
        return 'Mensagem trocada.'***

The application worked. However, I ask: the code is the right way?

  • I do it often, I usually use class inheritance and it works which is a beauty

  • From what I’ve seen it’s all right.

  • Yes, but I’m not able to return the 'Message exchanged' to the GUI, I tried to put the root, etc, but I could not. All that is missing is this <way? > detail. It turns out I still don’t quite understand the question of classes, instantiating, passing, self, etc.

  • Elton Nunes and Jeanextreme002, I’ve made some changes to the code, if you’d be so kind.

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.