访问另一个类Tkinter中的属性-OOP

时间:2019-01-10 19:49:03

标签: python oop tkinter

我正在尝试使用Tkinker开发自己的GUI应用程序,并遇到以下问题:

我无法通过使用负责db的另一个类数据库中的函数来从Window类的EntryDateVaraiable类中获取AddPage的热议。

我已经尝试使用app.EntryDateVaraibleapp.container.EntryDateVaraibleapp.Frame.EntryDateVaraible ...的几乎任何组合...

并始终收到类似以下错误:

Exception in Tkinter callback
Traceback (most recent call last):
  File "/usr/lib/python2.7/lib-tk/Tkinter.py", line 1544, in __call__
    return self.func(*args)
  File "/home/noxiss/Programming/PythonProgramms/GitHub/Budget_GUI/tk_test.py", line 254, in <lambda>
    AddButton = Button(self,text='Add', command=lambda: DB.Add())
  File "/home/noxiss/Programming/PythonProgramms/GitHub/Budget_GUI/tk_test.py", line 28, in Add
    (str(app.container.AddPage.EntryDateVariable.get()), str(Type.get()), str(Category.get()), str(EntryAmount.get()), str(Payment.get())))
  File "/usr/lib/python2.7/lib-tk/Tkinter.py", line 1906, in __getattr__
    return getattr(self.tk, attr)
AttributeError: container

代码:

class Database:

    db = sql.connect('Budget.db')
    c = db.cursor()

    def CreateTable(self):

        self.c.execute('''CREATE TABLE IF NOT EXISTS dane(ID INTEGER PRIMARY KEY, Date DATE,
         Type TEXT, Category TEXT, Amount REAL, Payment TEXT)''')

    def Commit(self):

        self.db.commit()

    def Add(self):


        self.c.execute('INSERT INTO dane(Date, Type, Category, Amount, Payment) VALUES(?,?,?,?,?)',
            (str(app.EntryDateVariable.get()), str(Type.get()), str(Category.get()), str(EntryAmount.get()), str(Payment.get())))

class Window(Tk):

    def __init__(self):
        #creates container for all pages
        Tk.__init__(self)
        container = Frame(self)
        container.pack(side="top", fill="both", expand = "True")

        container.grid_rowconfigure(0,weight=1)
        container.grid_columnconfigure(0,weight=1)

        menubar = MenuBar(container)
        filemenu = Menu(menubar, tearoff=0)
        filemenu.add_command(label='save settings',)
        self.config(menu=menubar)



        #creates dictionary for list of all pages
        self.frames = {}
        #creates the pages from the dictionary
        for F in (StartPage, MainPage, AddPage):
            frame = F(container, self)
            self.frames[F] = frame
            frame.grid(row=0, column=0,sticky='nsew')

        self.show_frame(StartPage)

    def show_frame(self,cont):

        frame = self.frames[cont]
        frame.tkraise()
class AddPage(Frame):


    def __init__(self,parent,controller):
            Frame .__init__(self,parent)

            EntryDateVariable = StringVar()
            EntryDateVariable .set(DateFunctions.CurrentDate)       

            EntryDate = Entry(self,textvariable=EntryDateVariable)
            EntryDate.grid(row=0,column=1)

1 个答案:

答案 0 :(得分:0)

  

问题:正在访问另一个Tkinter类中的属性

不要这样,让GUI中的class Database 独立
而是将数据作为<data sequence>传递到DB.Add(...

  1. def Add(...更改为

    def Add(self, data_seq):
        self.c.execute('INSERT INTO dane(Date, ...) VALUES(?, ...)', data_seq)
    
  2. class Database中创建class Window的实例变量 == controller

    class Window(Tk):
        def __init__(self):
            ...
            self.db = Database()
    
  3. 使EntryDateVariableclass AddPage的实例变量

    self.EntryDateVariable = StringVar()
    
  4. 将方法def insert_data(self)添加到class AddPage

    def insert_data(self):
        data_seq = [self.EntryDateVariable.get(), ...]
        self.controller.db.Add(data_seq)