5

This is my first post here!

First here's the github link (I'm noob on github too) of my project.

Edited:

So here is a example of what I want to do, I have a big Tkinter class with frames,labels,menus,buttons and all and some functions in it too.

I want to let the UI description in my MakeUI() and move my funcs to another file, but I need to still have access to widgets.

< Main.py >

# -*- coding: utf-8 -*-

from tkinter import *
from Interface import *


Fenetre = Tk()
UI = MakeUI(Fenetre)

UI.mainloop()

UI.destroy()

< Interface.py >

# -*- coding: utf-8 -*-

from tkinter import *
from tkinter.filedialog import *


class MakeUI(Frame):

    def __init__(self, Fenetre, **kwargs):

        # Héritage
        Frame.__init__(self, Fenetre, width=1500, height=700, **kwargs)

        self.pack(fill=BOTH)

        self.FrameInfos = LabelFrame(self, text="Choix des paramètres", padx=2, pady=2)
        self.FrameInfos.pack(fill="both", expand="yes", padx=5, pady=5)

        self.MsgInfosCarte = Label(self.FrameInfos, text="Example", width=45)
        self.MsgInfosCarte.pack(padx=2, pady=2)

    def AfficherCarte(self):
        self.MsgInfosCarte["text"] = "OK"

And now in this example, I need to move the AfficherCarte function to another file like MapFuncs.py or else. And I want MakeUI to be able to call other files funcs and other files funcs to modify the interface.

I can't manage to do it properly.

Thanks for your help.

eyllanesc
  • 235,170
  • 19
  • 170
  • 241
Banadora
  • 77
  • 1
  • 7
  • Are you asking for advice on how to split up the big `MakeUI` class? – turnip Apr 04 '17 at 13:33
  • Yeah exactly ! Like creating a MapFuncs.py with Map functions like the last one AfficherCarte. – Banadora Apr 04 '17 at 13:41
  • Please revise your post so there is an actual question. You will want to put the specific code you are asking about into the question, don't just link to a repository. Be specific as to what you want help with. – tkerwin Apr 04 '17 at 13:44
  • Please read and follow the advice here: [How to create a Minimal, Complete, and Verifiable example](http://stackoverflow.com/help/mcve). You probably only need a dozen or so lines of code split between two files to illustrate the general problem of how to split tkinter code across files. – Bryan Oakley Apr 04 '17 at 13:58
  • Edited, I hope it's OK – Banadora Apr 04 '17 at 14:19

1 Answers1

3

In order to move a function modifying a widget of your GUI in a separate file, you could simply pass the widget instance (or an object in which this instance is stored) as an input argument of your function:

< MapFuncs.py >

def AfficherCarte(UI):
    UI.MsgInfosCarte["text"] = "OK"

< Interface.py >

import tkinter as tk
from MapFuncs import AfficherCarte

class MakeUI(tk.Frame):

    def __init__(self, Fenetre, **kwargs):

        # Héritage
        tk.Frame.__init__(self, Fenetre, width=1500, height=700, **kwargs)
        self.pack()

        self.FrameInfos = tk.LabelFrame(self, text="Choix des paramètres", padx=2, pady=2)
        self.FrameInfos.pack(fill="both", expand="yes", padx=5, pady=5)

        self.MsgInfosCarte = tk.Label(self.FrameInfos, text="Example", width=45)
        self.MsgInfosCarte.pack(padx=2, pady=2)

        # Call function from external file to modify the GUI
        AfficherCarte(self)

If you're doing this because your code is getting too large, another way would be to divide your GUI into separate classes for each major part of the interface (see https://stackoverflow.com/a/17470842/4084269).

Community
  • 1
  • 1
Josselin
  • 2,593
  • 2
  • 22
  • 35