0

I am making a GUI and to tidy it up, I'm looking into the possibility of pulling the core functions out into an external script, and having the main GUI call this.

The external module needs to be able to read widget data, and manipulate it.

So for example:

I have a script that has the base of the GUI and when ran, opens it up.

I have another script, inside a folder, which will contain the modules to make the GUI function.

The main GUI script needs to be able to call these modules/functions, and they need to be able to edit the widgets.

I have done some research and none of it is helping, most of the questions are about functions that are in the main script, and I already know how to do this.

Main GUI Script

import os, sys, ttk, pyodbc

from Tkinter import *
import Utils.Common as Common

class GUI:
    def __init__(self, *args):
        self.style = ttk.Style()
        font = "TkDefaultFont"

        root.title(uiTitle)
        root.iconbitmap(uiIcon)
        root.geometry(uiGeometry)

        self.connectBtn = ttk.Button(root, width=65)
        self.connectBtn.configure(text="Connect", command=Common.connectCleardox)
        self.connectBtn.grid(row=0, columnspan=4)

        self.exampleEnt = Entry(root, width=30)
        self.exampleEnt.grid(row=1, columnspan=4)

if __name__ == '__main__':
    root = Tk()
    GUI(root)
    root.mainloop()

Utils/Common.py

import tkMessageBox
from Tkinter import *

sys.path.insert(0, r'\\cd3\IT\Python\#PythonLibraries\Modules')
from CommonDB import Database

def connectCleardox():
    try:
        cleardoxConnection = Database.ConnectDatabase(Database(r"\\cd3\IT\Python\#PythonLibraries\ConfigFiles\Cleardox\Cleardox ConfigFile.json"))
        exampleEnt.insert(0, 'Success') #The widget from the GUI
    except Exception as e:
        tkMessageBox.showerror('uiTitle', "Database connection failed.\n\n%s" % (e))
        exampleEnt.insert(0, 'Fail') #The widget from the GUI
        return

Please let me know if I am being too vague, or not giving enough description.

Thank you all for your time.

Dylan.

Dylan Logan
  • 395
  • 7
  • 18
  • Possible duplicate of [How to get variable data from a class](https://stackoverflow.com/questions/32212408/how-to-get-variable-data-from-a-class) – stovfl Oct 08 '19 at 10:11
  • So, your exact question is? Perhaps you could take a look over the MVC architecture pattern, I think it could help you to have a wider view of the system and properly structure your application. – danrodlor Oct 08 '19 at 10:13
  • Are you receiving a specific error message when trying to do this? What is your exact question that you need help with? – scotty3785 Oct 08 '19 at 10:26
  • Does your utils folder contain an `__init__.py` file? – scotty3785 Oct 08 '19 at 10:27
  • @danlor My exact question is how do I manipulate an Tkinter Object from an external module. – Dylan Logan Oct 08 '19 at 12:28
  • @scotty3785 I'm not receiving an error, please refer to my above comment. – Dylan Logan Oct 08 '19 at 12:29
  • @scotty3785 I do have an init file in my modules folder yeah, the import works fine, this is not the problem. Sorry for any inconvenience. – Dylan Logan Oct 08 '19 at 12:29
  • @DylanLogan Got it. You need to pass the connectCleardox function the tkinter widget so that it knows what `exampleEnt` is. `exampleEnt` isn't in the scope of the connectCleardox function so you need to tell it where it is. – scotty3785 Oct 08 '19 at 12:36
  • @DylanLogan Then I recommend you to check out MVC, again. – danrodlor Oct 08 '19 at 12:41

0 Answers0