【问题标题】:Retrieving an object function tkinter检索对象函数 tkinter
【发布时间】:2017-07-01 11:31:40
【问题描述】:
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”来查找数据基础,无需按下“下一步”按钮。

【问题讨论】:

    标签: python python-3.x tkinter sqlite


    【解决方案1】:

    您调用的是 PagOne (拼写错误?),而不是从数据库创建类的实例。试试下面的。它演示了调用 PageOne.any 函数。

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

    【讨论】:

      猜你喜欢
      • 2015-02-20
      • 1970-01-01
      • 2020-03-31
      • 2014-04-04
      • 2016-03-18
      • 1970-01-01
      • 1970-01-01
      • 2011-11-22
      • 1970-01-01
      相关资源
      最近更新 更多