重新启动后Tkinter变慢

时间:2019-10-07 14:50:24

标签: python-3.x tkinter fedora tk

我在python3中遇到了库 Tkinter

我渲染了许多小部件,然后再次关闭程序。 每次我再次启动程序时,小部件的渲染都会变得。 我真的很感谢解决方案。

展示:

import tkinter
from datetime import datetime
from threading import Thread
from tkinter import BOTH, VERTICAL, HORIZONTAL


def __workspace_frame_resize__(event):
    canvas_for_workspace.configure(
        scrollregion=canvas_for_workspace.bbox("all"))

def render():
    start = datetime.now()
    for i in range(0, 2000):
        lbl = tkinter.Label(workspace, text="Test " + str(i))
        lbl.pack(side=tkinter.TOP, expand=True, fill=tkinter.X)
    end = datetime.now()
    print("Rendered in "+str(end-start)+"s")

def create_scroll_bar(orient, command, window=None):
    if window is None:
        scrollbar = tkinter.Scrollbar(root, orient=orient,
                                      command=command, background="white", cursor="hand1")
    else:
        scrollbar = tkinter.Scrollbar(window, orient=orient,
                                      command=command, background="white", cursor="hand1")
    if orient == HORIZONTAL:
        scrollbar.pack(side=tkinter.BOTTOM, fill=tkinter.X)
    elif orient == VERTICAL:
        scrollbar.pack(side=tkinter.RIGHT, fill=tkinter.Y)
    else:
        Exception("BAD ORIENTATION")
    return scrollbar

root = tkinter.Tk()
canvas_for_workspace = tkinter.Canvas(root, bg="white",
                                      highlightthickness=0)
vscrollbar = create_scroll_bar(VERTICAL,
                               canvas_for_workspace.yview)
hscrollbar = create_scroll_bar(HORIZONTAL,
                               canvas_for_workspace.xview)
canvas_for_workspace.pack(side=tkinter.LEFT, fill=BOTH, expand=True)
workspace = tkinter.Frame(canvas_for_workspace, bg="white")
workspace.pack(side=tkinter.LEFT, fill=BOTH, expand=True)
workspace.bind("<Configure>", __workspace_frame_resize__)
canvas_for_workspace.configure(yscrollcommand=vscrollbar.set,
                               xscrollcommand=hscrollbar.set)
canvas_for_workspace.create_window((0, 0), window=workspace,
                                   anchor="nw")
Thread(target=render).start()
root.mainloop()

0 个答案:

没有答案