[英]Accessing attribute in another class Tkinter - OOP
I am trying to develop my own GUI application with Tkinker and struggling with the following problem: 我正在尝试使用Tkinker开发自己的GUI应用程序,并遇到以下问题:
I cannot figure out hot to get the EntryDateVaraiable
from AddPage
class that is a part of Window class using a function from another class Database that is responsible for working with db. 我想不通使用另一个负责数据库工作的类数据库中的函数,从
AddPage
类中获取EntryDateVaraiable
,该类是Window类的一部分。
I've already tried with almost any combination of app.EntryDateVaraible
, app.container.EntryDateVaraible
, app.Frame.EntryDateVaraible
... 我已经尝试使用
app.EntryDateVaraible
, app.container.EntryDateVaraible
, app.Frame.EntryDateVaraible
几乎任何组合...
and always receives errors like: 并始终收到以下错误:
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
Code: 码:
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)
Question : Accessing attribute in another class Tkinter
问题 :在另一个类Tkinter中访问属性
Don't go this way, let the class Database
independent from the GUI. 不要这样,让
class Database
独立于GUI。
Pass the data as <data sequence>
to DB.Add(...
instead. 将数据作为
<data sequence>
传递给DB.Add(...
Change def Add(...
to 将
def Add(...
更改为
def Add(self, data_seq): self.c.execute('INSERT INTO dane(Date, ...) VALUES(?, ...)', data_seq)
Create a instance variable of class Database
in class Window
== controller
在
class Window
== controller
创建class Database
的实例变量
class Window(Tk): def __init__(self): ... self.db = Database()
Make EntryDateVariable
a instance variable of class AddPage
将
EntryDateVariable
设为class AddPage
的实例变量
self.EntryDateVariable = StringVar()
Add the methode def insert_data(self)
to class AddPage
将方法
def insert_data(self)
到class AddPage
def insert_data(self): data_seq = [self.EntryDateVariable.get(), ...] self.controller.db.Add(data_seq)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.