簡體   English   中英

如何使用許多窗口和功能來組織Tkinter代碼

[英]How to organize Tkinter code with many windows and functions

我正在使用Python和Tkinter構建一個數據分析程序,該程序允許輸入,打印和導出數據(一種Excel克隆)。 它實現了許多需要訪問和修改共享數據變量的窗口和功能。 我應該如何組織GUI以確保每個窗口都可以來回傳輸數據,同時還要分隔代碼以能夠獨立修改和調試每個功能?

最初,我建立了一個帶有子功能的主GUI類(示例1),由於所有變量都是通過self共享的,因此所有功能很容易修改和訪問初始化的數據。 但是,隨着我越來越多地修改代碼(實際上是數千行),由於代碼的划分不正確,調試變得非常困難。

我看到一個建議,使每個窗口(即數據表,繪圖窗口等)成為獨立的類(示例2),但是我想出了如何在類之間傳遞數據的唯一方法(將第一個窗口對象傳遞給其他窗口) )看起來非常混亂,我唯一能想到的選擇是在定義/調用時將所有必需的變量顯式傳遞給每個窗口,這也可能非常混亂。

例子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()

例子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 """

如何在保留類之間簡單的變量交換的同時還允許我分別隔離和調試每個多窗口程序的方式來改進此多窗口程序的組織?

非常感謝你。

問題 :如何在類之間傳遞數據

該解決方案不通過任何高炮類,它簡單的采用了全球 class DATA對象。 也可以將此對象保存在自己的DATA.py ,以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()

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM