检索对象函数tkinter

时间:2017-07-01 11:31:41

标签: python python-3.x tkinter sqlite

class App_1(tk.Tk):
    def __init__(self, *args, **kwargs):
        tk.Tk.__init__(self, *args, **kwargs)
        container = ttk.Frame(self)
        container.pack(side="top", fill="both", expand=True)   
        container.grid_rowconfigure(0, weight=1)
        container.grid_columnconfigure(0, weight=1)

        self.frames = {}
        for F in (PageOne, PageTwo):
           frame = F(container, self)
           self.frames[F] = frame
           frame.grid(row=0, column =0, sticky="nsew")
        self.show_frame(PageOne)

    def show_frame(self, cont):
        frame = self.frames[cont]
        frame.tkraise()

class PageOne(tk.Frame):
    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)
        self.lf_empresa = ttk.Labelframe(self, text="Empresa")
        self.lf_empresa.grid(row=1, column=1)
        self.create_lf_empresa() 

    def create_lf_empresa(self):
        self.t, self.i, self.id = tk.StringVar(), tk.StringVar(), 0             
        self.l_empresa = tk.Label(self.lf_empresa, textvariable = self.t, fg = "blue", font = VARIABLE_FONT)
        self.l_empresa.grid(row=0, column = 1, sticky="SW")
        self.next_button = ttk.Button(self.lf_empresa, text="next", command=lambda: self.new_id())
        self.next_button.grid(row=1, column=2, sticky="SW")
        self.id_label = tk.Label(self.lf_empresa, textvariable = self.i , fg = "blue", font = VARIABLE_FONT)
        self.id_label.grid(row=0, column = 2, sticky="SW")

     def new_id(self):
         self.id = self.id + 1
         self.update_variables()

     def update_variables(self):
         database.select_data(self.id)
         self.t.set(database.data["empresa"])   
         self.i.set(str(self.id))


class PageTwo(tk.Frame):
            ***


class DataBase():
    def __init__(self):
        self.db = sq.connect("test_1.sqlite")
        self.cursor =self.db.cursor()
    def select_data(self, i):
        self.data = {"empresa": ""}
        self.cursor.execute("select  empresa from empresas 
        where id_e = ?", (i, )); empresa = self.cursor.fetchone()

        if empresa != None:
           self.data["id_e"] = empresa[0]
        else:
          **HERE IS WHERE I WANT TO GO TO new_id in PageOne**


database = DataBase()       
app = App_1()
app.mainloop()

我只想从else部分的DataBase对象调用new_id函数,尝试跳过没有" id"在数据库上,所以界面将获得新的" id"查看数据库,而无需按下按钮" next"。

1 个答案:

答案 0 :(得分:0)

您正在调用PagOne (拼写错误?),而不是从DataBase创建类的实例。请尝试以下方法。它演示了PageOne.anyfunction被调用。

class PageOne(Tk.Frame):
    def anyfunction(self):
        print("In PageOne anyfunction")

class DataBase():
    data = None
    if data == None:
        # Create an INSTANCE of PageOne.
        p = PageOne()
        # Call the anyfunction of the PageOne object.
        p.anyfunction()