Why does tkinter Scale widget have such a slow response?

1.1k views Asked by At

I've got my head around the code, but I can't understand why the scale is so unresponsive/slow.

from tkinter import *

#main menu
def mmWindow():
    mmWindow=Tk()
    mmWindow.geometry('600x600')

#Page 1
def page1():
    master.title('Page 1')
    # set up a boolean for each page, when page click turn one boolean true and the rest false
    #look at frames, canvas looks like the main option
    #draw sliders 1-6
    Scale(master, from_=0, to=100).place(x=200, y=120)

#Page 2
def page2():
    master.title('Page 2')
    #draw sliders 7-12
    Scale(master, from_=0, to=100).place(x=300, y=120)
#Page 3
def page3():
    master.title('Page 3')
    #draw slider 13-18

#Page 4
def page4():
    master.title('Page 4')
    #draw sldiers 19-24

#Presets
def presets():
    master.title('Presets')

#settings
def settings():
    master.title('Settings')


#first window   
master= Tk()
master.geometry('1440x900+0+0')    
master.title('DMX512 Controller')

#buttons
bw=250
bh=110
img1 = PhotoImage(file="/Users/Josh/Documents/Uni/Year3/Diss/Images/Gif/mainmenu.gif")
img2 = PhotoImage(file="/Users/Josh/Documents/Uni/Year3/Diss/Images/Gif/p1.gif")
img3 = PhotoImage(file="/Users/Josh/Documents/Uni/Year3/Diss/Images/Gif/p2.gif")
img4 = PhotoImage(file="/Users/Josh/Documents/Uni/Year3/Diss/Images/Gif/p3.gif")
img5 = PhotoImage(file="/Users/Josh/Documents/Uni/Year3/Diss/Images/Gif/p4.gif")
img6 = PhotoImage(file="/Users/Josh/Documents/Uni/Year3/Diss/Images/Gif/presets.gif")
img7 = PhotoImage(file="/Users/Josh/Documents/Uni/Year3/Diss/Images/Gif/settings.gif")

Button(master, image=img1, command =mmWindow, width=bw, height=bh).place(x=1190,y=0)
Button(master, image=img2,command =page1).place(x=1190,y=120)
Button(master, image=img3, command =page2).place(x=1190,y=240)
Button(master, image=img4,command =page3).place(x=1190,y=360)
Button(master, image=img5, command =page4).place(x=1190,y=480)
Button(master, image=img6,command =presets).place(x=1190,y=600)
Button(master, image=img7,command =settings).place(x=1190,y=720)


#text
wtitle = Label (master, text = "Pi DMX", fg = 'blue')
wtitle.place(x = 640, y = 100)

master.mainloop()

(I know, I've not gone about writing my code in the most eco friendly way, too.)

2

There are 2 answers

1
Jordanian On

If you want the size of the window to change use .geometry

master.geometry('400x400')
0
mart On

I had the same issue, and created a (rather ugly) workaround that created a wrapper around the callback functions, so that the update() method of the Frame the Scale is placed in was called everytime the callback function was called. If you don't use a callback function, you could pass the self.update method directly to the command argument of the Scale initializer.

This method works, but feels unsatisfying and hackish.

import tkinter as tk

class MainWindow(tk.Frame):

    def __init__(self, master=None, callback_funcs=None, max_rows=8):
        tk.Frame.__init__(self, master)
        self.grid()
        cb = lambda callback: lambda val: self.run_callback(callback, val) 
        self.callback_funcs = [cb(callback) for callback in callback_funcs]
        self.max_rows = max_rows
        self.create_scales()

    def run_callback(self, callback, val):
        self.update()
        callback(val)

    def create_scales(self):
        self.scales = []
        for callback in self.callback_funcs:
            scale = tk.Scale(from_=0, to=1, resolution=0.01, command=callback, orient=tk.HORIZONTAL)
            scale.grid()