tkinter让一个类运行一个函数来刷新MYSQL的数据

时间:2019-02-10 19:46:16

标签: python tkinter

我正在编写的tkinter中有一个GUI,它基于MYSQL数据库,这是我的新手,我写了一个页面来显示数据库(Dash)中的数据,还有其他页面可以添加数据存储到数据库中,问题是每次我回到“ Dash”页面时,它仅显示其开始的原始数据

不确定我是否正确地放置了代码,但是它可以工作。

如果代码没有像其他页面一样显示,则为自定义按钮的字典等。

我尝试将frame.update()添加到showframe函数中。

我尝试编写一个函数来查找数据并刷新,但是每次显示框架时都无法调用它。

def connection_check_all(query, values):
    mydb = mysql.connector.connect(
        host="",
        user="",
        passwd="",
        database=""
    )

    mycursor = mydb.cursor()
    mycursor.execute(query, (values, ))
    myresult = mycursor.fetchall()
    mydb.close()
    return myresult

class AnimalManagment(tk.Tk):

    def __init__(self, *args, **kwargs):
        tk.Tk.__init__(self, *args, *kwargs)
        tk.Tk.config(self, bg="white")
        tk.Tk.geometry(self, "800x480")
        tk.Tk.title(self, "Animal Managment")
        container = tk.Frame(self)
        container.pack(side="top", fill="both", expand=True)
        container.grid_rowconfigure(0, weight=1)
        container.grid_columnconfigure(0, weight=1)

        # dictionary of frames

        self.frames = {}

        # creates the frames in for loop

        for F in (Dash, MainPage):
            frame = F(container, self)
            self.frames[F] = frame
            frame.grid(row=0, column=0, sticky="nsew")
            frame.config(background="white")

        self.show_frame(StartPage)

    # show frame definition

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

class Dash(tk.Frame):

    def __init__(self, parent, controller):

        tk.Frame.__init__(self, parent)

    # fetching data for varables in table

        query = "select count(*) from sheep where on_farm = %s"
        value = "1"
        self.no_animals = connection_check_all(query, value)

        query = "select count(*) from sheep where sex = %s"
        value = "F"
        self.no_ewes = connection_check_all(query, value)

        query = "select count(*) from sheep where sex = %s"
        value = "M"
        self.no_rams = connection_check_all(query, value)

        # Widgets in Dash

        self.table = Table(self, ["List", "Values"], column_minwidths=          [None, None])
        self.table.pack(expand=True, fill=X, padx=10, pady=10)

        button = tk.Button(self, custom_button, text="back",     command=lambda: controller.show_frame(MainMenuSheep))
        button.pack()

        self.table.set_data([["Number of Animals", self.no_animals],
                             ["Number of Ewes", self.no_ewes],
                             ["Number of Rams", self.no_rams],
                             ])

我希望的是,每次我调用该页面时,都有一些方法使我能够获得自动运行以更新数据的功能,因为我在一些页面上都遇到了此问题。

0 个答案:

没有答案