0

这是我正在编写的程序的简化版本,并且可以正常工作。我使用最后一个按钮进行测试以查看我的小说作品,并且确实如此。

我需要做

self.controller.show_orders 

功能每隔几秒自动运行一次,而不是每隔几秒按一次按钮。

我试图取出制作按钮的代码,只有功能,但它不运行。

我也试过

        self.show_orders_on_grid()
    def show_orders_on_grid(self):
        self.controller.show_orders

它也不起作用。

我最后的努力是将主循环放在函数之后,这很有效,我明白为什么。

        for i in range(len(self.model.row_list)):
            self.view.listbox_list[i].delete(0, "end",)
            for element in self.model.row_list[i]:
                self.view.listbox_list[i].insert("end", element)

        self.root.mainloop()
if __name__ == "__main__":
    c = Controller()

你能帮我如何让这个功能自己运行吗?先感谢您。

from datetime import datetime
import time
import os
import tkinter as tk
import sqlite3
from apscheduler.schedulers.background import BackgroundScheduler
import socket
import json
import tkinter as tk

class Model():
    def __init__(self):

        self.row_list = []
        self.path = '/Users/paul/Documents/python/GUI/gui class/ordering 
                     app/4th.db'

        self.get_data_from_sql()
    def get_data_from_sql(self):
        self.conn = sqlite3.connect(self.path)
        self.cursor = self.conn.cursor()
        self.cursor.execute('SELECT * FROM main')
        self.data = self.cursor.fetchall()
        for row in self.data:
            self.row_list.append(row)
        self.conn.commit()


class View(tk.Frame):
    def  __init__(self, parent, controller):
        super().__init__(parent, bd =2, relief = tk.RIDGE)
        self.parent = parent
        self.controller = controller
        self.pack(fill=tk.BOTH, expand=1)

        self.frame_list = []
        self.listbox_list = []
        self.button_list = []


        for r in range(2):
            self.rowconfigure(r, weight=1)
            for c in range(4):
                self.columnconfigure(c, weight=1)
                self.frame=Small_Frame(self)
                self.frame.grid(row = r, column = c, padx=1, pady = 1,  sticky=
                               (tk.N, tk.S, tk.W, tk.E))
                self.frame_list.append(self.frame)


        self.button_list[7].configure(command = self.controller.show_orders)

class Small_Frame(tk.Frame):
    def __init__(self, parent):
        super().__init__(parent, borderwidth=1, relief="groove")
        self.parent = parent
        self.pack()

        self.box = tk.Listbox(self, borderwidth=0)
        self.box.pack()
        self.parent.listbox_list.append(self.box)

        self.button = tk.Button(self, text = "get")
        self.button.pack()
        self.parent.button_list.append(self.button)

class Controller():
    def __init__(self):
        self.root = tk.Tk()
        self.model = Model()
        self.view = View(self.root, self)
        self.root.title("notbook my own try")
        self.root.geometry("1200x650")
        self.root.config(bg="LightBlue4")
        self.root.mainloop()

    def show_orders(self):
        for i in range(len(self.model.row_list)):
            self.view.listbox_list[i].delete(0, "end",)
            for element in self.model.row_list[i]:
                self.view.listbox_list[i].insert("end", element)

if __name__ == "__main__":
    c = Controller()
4

1 回答 1

1

您可以使用重复after()执行:show_orders()

class Controller:
    def __init__(self):
        ...
        self.show_orders()
        self.root.mainloop()

    def show_orders(self):
        ...
        # call show_orders every 5 seconds
        self.root.after(5000, self.show_orders)
于 2020-02-17T03:56:17.903 回答