简体   繁体   中英

How to organize Tkinter code with many windows and functions

I am building a data-analysis program using Python and Tkinter that allows for the entry, plotting and export of data (an Excel clone of sorts). It implements many windows and functions that need to access and modify shared data variables. How should I organize the GUI in order to ensure that each window can transfer data back and forth, while also compartmentalizing the code to be able to modify and debug each function independently?

I initially built a main GUI class with sub-functions (Example 1), where it is easy for all of the functions to modify and access the data initialized, as all variables are shared through self. However, as I increasingly modify the code (which is actually thousands of lines long), it became challenging to debug as the code isn't compartmentalized well.

I saw a suggestion to have each window (ie data table, plot window, etc) be independent classes (Example 2), but the only way I figured out how to pass the data between classes (passing the first window object to the other windows) seems very messy, and the only alternative I could figure out was explicitly passing all necessary variables to each window at the definition/call, which can also be very messy.

Example 1

class GUI:
    def __init__(self, toptk):
        self.toptk = toptk
        """ Code creating main window and toolbar """
        list_x = []
        list_y = []
        """ Code adding data """
    def entryTable(self):
        tablewin = Toplevel(self.toptk)
        """ Code creating frames, buttons, etc """
        """ Code doing something to list_x, list_y """
    def plotWin(self):
        plotwin = Toplevel(self.toptk)
        """ Code creating frames, buttons, etc """
        """ Code doing something to list_x, list_y """
    entryTable()
    plotWin()

root = tk.Tk()
main = GUI(root)
root.mainloop()

Example 2

class GUI:
    def __init__(self, toptk):
        """ Code creating main window and toolbar """
        list_x = []
        list_y = []
        """ Code adding data """
        entryTable.__init__(toptk,self)
        plotWin.__init__(toptk,self)
class entryTable():
    def __init__(self,toptk,topGUI):
        tabletop = Toplevel(toptk)
        """ Code creating frames, buttons, etc """
        """ Code doing something to topGUI.list_x, topGUI.list_y """
class plotWin():
    def __init__(self,toptk,topGUI):
        plottop = Toplevel(toptk)
        """ Code creating frames, buttons, etc """
        """ Code doing something to topGUI.list_x, topGUI.list_y """

How can I improve the organization of this multi-window program in a way that retains a simple exchange of variables between classes while also allowing me to isolate and debug each one individually?

Thank you very much.

Question : how to pass the data between classes

This solution don't pass anything beteen classes, it simple uses a global class DATA object. It's also possible to hold this object in a own DATA.py , to do import DATA .

import tkinter as tk


class DATA:
    """ Data container """
    list_x = []
    list_y = []


class EntryTable(tk.Toplevel):
    def __init__(self, parent):
        super().__init__(parent)

        """ Code creating frames, buttons, etc """

        """ Code adding something to DATA.list_x, DATA.list_y """
        DATA.list_x = [1, 2, 3]
        DATA.list_y = [4, 5, 6]


class PlotWin(tk.Toplevel):
    def __init__(self, parent):
        super().__init__(parent)

        """ Code creating frames, buttons, etc """

        """ Code doing something with DATA.list_x, DATA.list_y """
        print('plot:{}'.format((DATA.list_x, DATA.list_y)))


class App(tk.Tk):
    def __init__(self):
        super().__init__()

        """ Code adding main window widgets and toolbar """

        """ Code adding Toplevel window's """
        entryTable = EntryTable(self)
        plotWin = PlotWin(self)


if __name__ == '__main__':
    App().mainloop()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM