Sluggish Tkinter behavior

Here is a simple GUI program that creates a 5x16 matrix of buttons.

from tkinter import *

root = Tk()
button = [[0 for x in range(16)] for x in range(5)]
for r in range(5):
    for c in range(16):
        button[r][c] = Button(root,  bg='red')
        button[r][c].grid(row=r, column=c)       
root.mainloop()

      

Now when I run this program on my relatively well Ubuntu Laptop (Ram 4 GB, Intel Pentium (R) N3540 quad core @ 2.16 GHz × 4, 64 bit),

The buttons render noticeably slowly, one by one, as shown in this file:

enter image description here

Are you asking to draw 80 buttons too many to ask Tkinter to handle without this slowness?

Or can my code be refactored for better performance?

+3


source to share


1 answer


If the code in your question is literally everything, there must be something wrong with your system. It should appear almost instantly.



And no, there is no way to refactor for better performance. If you want a grid of 80 buttons, the way you do it is about as effective as you can do it.

+1


source







All Articles