Kinetic Scrolling and Keyboard Shortcuts in Menus

View: New views
4 Messages — Rating Filter:   Alert me  

Kinetic Scrolling and Keyboard Shortcuts in Menus

by Johnston Jiaa-2 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

I'm having trouble trying to make a kinetic scrolling canvas.  The  
code looks something like this...

def scroll(acceleration, speed):
        while speed >= 0:
                canvas.xview_scroll(-speed, "units")
                speed -= acceleration

This doesn't update the drawing of the canvas in real-time (as in,  
every time xview_scroll is called)... it only updates the drawing  
after the while loop has already run, so you don't get to see it  
scroll, which is stupid.  How can I get this to work properly?


Also, I have set keyboard shortcuts for some functions in my program.  
The functions these shortcuts call are also linked to menu items in  
the menu bar.  How can I get the menu labels to reflect the keyboard  
shortcuts for each relative command?  I know I can set the shortcut as  
a string in the label manually, but it will be stupid, not flush with  
the right edge of the menu, like in other programs.


Thanks for helping my program not be stupid
_______________________________________________
Tkinter-discuss mailing list
Tkinter-discuss@...
http://mail.python.org/mailman/listinfo/tkinter-discuss

Re: Kinetic Scrolling and Keyboard Shortcuts in Menus

by Guilherme Polo :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

On Sun, Aug 31, 2008 at 1:11 AM, Johnston Jiaa <oclbdk@...> wrote:

> I'm having trouble trying to make a kinetic scrolling canvas.  The code
> looks something like this...
>
> def scroll(acceleration, speed):
>        while speed >= 0:
>                canvas.xview_scroll(-speed, "units")
>                speed -= acceleration
>
> This doesn't update the drawing of the canvas in real-time (as in, every
> time xview_scroll is called)... it only updates the drawing after the while
> loop has already run, so you don't get to see it scroll, which is stupid.
>  How can I get this to work properly?

You will need a canvas.update() after that .xview_scroll
But, why aren't you using "after" instead of a while loop for this ?

>
>
> Also, I have set keyboard shortcuts for some functions in my program.  The
> functions these shortcuts call are also linked to menu items in the menu
> bar.  How can I get the menu labels to reflect the keyboard shortcuts for
> each relative command?  I know I can set the shortcut as a string in the
> label manually, but it will be stupid, not flush with the right edge of the
> menu, like in other programs.
>

There are actually two question here.

For the later the answer is.. instead of setting the shortcut it in
the label, use the accelerator option to set it.
The former is about reflecting keyboard shortcuts in menu labels, for
this you will need to roll your own solution or try finding someone
that already did it.

>
> Thanks for helping my program not be stupid
> _______________________________________________
> Tkinter-discuss mailing list
> Tkinter-discuss@...
> http://mail.python.org/mailman/listinfo/tkinter-discuss
>



--
-- Guilherme H. Polo Goncalves
_______________________________________________
Tkinter-discuss mailing list
Tkinter-discuss@...
http://mail.python.org/mailman/listinfo/tkinter-discuss

Re: Kinetic Scrolling and Keyboard Shortcuts in Menus

by Cam Farnell-2 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

I ran into exactly this keyboard shortcut issue while writing the Rapyd-Tk Python development environment (http://www.bitflipper.ca/rapyd/). My solution is based on these functions, where the left part is the usual menu label and the right part is the shortcut. If the length of the shortcut text varies a lot you can compute the width on the fly as is done in Rapyd, otherwise just use some reasonable constant.


def PadToWidth(LeftPart,RightPart,Width,Widget):
    """
    Given left and right parts, return string of specified width in pixels.
   
    o "LeftPart" is the string to be left justified in the result.
    o "RightPart" is the string to be right justified in the result.
    o "Width" is the desired width, in pixels, of the result.
    o "Widget" the calculation is done with reference to the font set for this widget.
   
    Note that since we pad with space characters, the result will be as close as
        possible to the target size but will not necessarily be exactly the number
        of pixels requested.
    """
    UsedPixels = TextMeasure(LeftPart+RightPart,Widget)
    PadPixels = Width - UsedPixels
    PadCount = int(round(float(PadPixels) / float(TextMeasure(' ',Widget))))
    return '%s%s%s'%(LeftPart,' '*PadCount,RightPart)

def TextMeasure(Text,Widget):
    """=u
    Measure size of "Text", in pixels, if displayed in "Widget".
    """
    return int(Widget.tk.call("font", "measure", Widget["font"]
        ,"-displayof", Widget.master, Text ))



Johnston Jiaa wrote:

>
> Also, I have set keyboard shortcuts for some functions in my program.  
> The functions these shortcuts call are also linked to menu items in the
> menu bar.  How can I get the menu labels to reflect the keyboard
> shortcuts for each relative command?  I know I can set the shortcut as a
> string in the label manually, but it will be stupid, not flush with the
> right edge of the menu, like in other programs.
>
>
> Thanks for helping my program not be stupid
_______________________________________________
Tkinter-discuss mailing list
Tkinter-discuss@...
http://mail.python.org/mailman/listinfo/tkinter-discuss

Re: Kinetic Scrolling and Keyboard Shortcuts in Menus

by Guilherme Polo :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

On Sun, Aug 31, 2008 at 3:47 PM, Cam Farnell <msa01@...> wrote:
> I ran into exactly this keyboard shortcut issue while writing the Rapyd-Tk
> Python development environment (http://www.bitflipper.ca/rapyd/). My
> solution is based on these functions, where the left part is the usual menu
> label and the right part is the shortcut. If the length of the shortcut text
> varies a lot you can compute the width on the fly as is done in Rapyd,
> otherwise just use some reasonable constant.
>

Why did you use that instead of using the accelerator option ? Ok, the
accelerator text doesn't go all the way to the right in the menu but
they get are all aligned and you don't have to worry about doing
anything else, and it is not that bad after all (not as I see at
least).

I'm attaching an image so you can confirm that you dislike its behavior.

>
> def PadToWidth(LeftPart,RightPart,Width,Widget):
>   """
>   Given left and right parts, return string of specified width in pixels.
>     o "LeftPart" is the string to be left justified in the result.
>   o "RightPart" is the string to be right justified in the result.
>   o "Width" is the desired width, in pixels, of the result.
>   o "Widget" the calculation is done with reference to the font set for this
> widget.
>     Note that since we pad with space characters, the result will be as
> close as
>       possible to the target size but will not necessarily be exactly the
> number
>       of pixels requested.
>   """
>   UsedPixels = TextMeasure(LeftPart+RightPart,Widget)
>   PadPixels = Width - UsedPixels
>   PadCount = int(round(float(PadPixels) / float(TextMeasure(' ',Widget))))
>   return '%s%s%s'%(LeftPart,' '*PadCount,RightPart)
>
> def TextMeasure(Text,Widget):
>   """=u
>   Measure size of "Text", in pixels, if displayed in "Widget".
>   """
>   return int(Widget.tk.call("font", "measure", Widget["font"]
>       ,"-displayof", Widget.master, Text ))
>
>
>
> Johnston Jiaa wrote:
>
>>
>> Also, I have set keyboard shortcuts for some functions in my program.  The
>> functions these shortcuts call are also linked to menu items in the menu
>> bar.  How can I get the menu labels to reflect the keyboard shortcuts for
>> each relative command?  I know I can set the shortcut as a string in the
>> label manually, but it will be stupid, not flush with the right edge of the
>> menu, like in other programs.
>>
>>
>> Thanks for helping my program not be stupid
>
> _______________________________________________
> Tkinter-discuss mailing list
> Tkinter-discuss@...
> http://mail.python.org/mailman/listinfo/tkinter-discuss
>


--
-- Guilherme H. Polo Goncalves


_______________________________________________
Tkinter-discuss mailing list
Tkinter-discuss@...
http://mail.python.org/mailman/listinfo/tkinter-discuss

menu_img.png (7K) Download Attachment
LightInTheBox - Buy quality products at wholesale price!