maemo.org - Talk

maemo.org - Talk (https://talk.maemo.org/index.php)
-   Development (https://talk.maemo.org/forumdisplay.php?f=13)
-   -   Python: scroll a page with keypress; help needed. (https://talk.maemo.org/showthread.php?t=28086)

BrentDC 2009-04-04 21:58

Python: scroll a page with keypress; help needed.
 
I have a python application that I want to implement key-press scrolling on. I already have all the main frame code in place, just have no idea how to scroll something.

Here is the important part of the code:

Code:

  def on_key_press(self, widget, event, *args):
      if event.keyval == gtk.keysyms.F6:
      # The "Full screen" hardware key has been pressed
        if self.window_in_fullscreen:
            self.window.unfullscreen()
        else:
            self.window.fullscreen()

      if event.keyval == gtk.keysyms.F7:
        self.zoom_in_button_callback(self.textview)

      if event.keyval == gtk.keysyms.F8:
        self.zoom_out_button_callback(self.textview)

This function is called when I catch the key-press-event, and as you probably know, the if statements catch the correct key and executes the appropriate function.

I know I need to add if statements to catch the Up/Down key buttons, but how to you scroll a textview and/or scrolledwindow?

The textview is self.textview and scrolledwindow is self.sw (to complicate matters, the scrolledwindow can either be a gtk.ScrolledWindow or mokoui.FingerScroll object).

Any help is appreciated.

GeraldKo 2009-04-04 22:31

Re: Python: scroll a page with keypress; help needed.
 
This is probably no help, but you know this?

http://www.internettablettalk.com/wi...e_Key_bindings

.

TrueJournals 2009-04-04 22:56

Re: Python: scroll a page with keypress; help needed.
 
Not going to write any test code, so I haven't tried this at all... First I think you'll need to test for whether it's a ScrolledWindow or FingerScroll... For a ScrolledWindow, use gethadjustment(), then set the value of the returned gtk.Adjustment to some increment above or below its current value, then use sethadjustment() on the ScrolledWindow.

I hope that made sense. Not sure about the FingerScroll.

[edit]Here's some mostly-code:
Code:

if event.keyval == (UP keysym):
    if type(self.sw) == type(gtk.ScrolledWindow()):
        adj = self.sw.get_vadjustment()
        val = adj.get_value()
        adj.set_value(val+(adjustment))
        self.sw.set_vadjustment(adj) # Not sure if this is necessary
    elif type(self.sw) == type(mokoui.FingerScroll()):
        pass
        #Not sure about this

Obviously, this leaves about some values, and I haven't looked into mokoui.FingerScrol. You would also want to test that mokoui is imported before you just call type(mokoui.FingerScroll())

[edit2]Looking at mokoui... I believe the code would be the same as the scrolledwindow code, so no need to type test. Again, I haven't tried this at all (I've never even dealt with mokoui before).

BrentDC 2009-04-05 01:05

Re: Python: scroll a page with keypress; help needed.
 
Quote:

Originally Posted by GeraldKo (Post 277498)
This is probably no help, but you know this?

http://www.internettablettalk.com/wi...e_Key_bindings

.

Yeah, that is kinda for Microb, this is different.

BrentDC 2009-04-05 01:38

Re: Python: scroll a page with keypress; help needed.
 
Quote:

Originally Posted by TrueJournals (Post 277501)
Not going to write any test code, so I haven't tried this at all... First I think you'll need to test for whether it's a ScrolledWindow or FingerScroll... For a ScrolledWindow, use gethadjustment(), then set the value of the returned gtk.Adjustment to some increment above or below its current value, then use sethadjustment() on the ScrolledWindow.

I hope that made sense. Not sure about the FingerScroll.

[edit]Here's some mostly-code:
Code:

if event.keyval == (UP keysym):
    if type(self.sw) == type(gtk.ScrolledWindow()):
        adj = self.sw.get_vadjustment()
        val = adj.get_value()
        adj.set_value(val+(adjustment))
        self.sw.set_vadjustment(adj) # Not sure if this is necessary
    elif type(self.sw) == type(mokoui.FingerScroll()):
        pass
        #Not sure about this

Obviously, this leaves about some values, and I haven't looked into mokoui.FingerScrol. You would also want to test that mokoui is imported before you just call type(mokoui.FingerScroll())

[edit2]Looking at mokoui... I believe the code would be the same as the scrolledwindow code, so no need to type test. Again, I haven't tried this at all (I've never even dealt with mokoui before).

I just tested the code, using gtk.SROLL_PAGE_UP as the adjustment value, and it works (with a couple issues). The only problem is that it scrolls by small margins, so I tried:

Code:

adj.set_page_increment(gtk.SCROLL_PAGES)
But it says "AttributeError: 'gtk.Adjustment' object has no attribute 'set_page_increment'", which is strange. Because the pygtk reference seems to disagree...Also mokoui.FingerSroll seems to have a completely different api, because it doesn't have a gtk.Adjustment. I'll figure that out, but the first thing is stumping me.

TrueJournals 2009-04-05 02:09

Re: Python: scroll a page with keypress; help needed.
 
Hmm... strange, but it seems to be a very surface-level problem. Try this:
Code:

adj.page_incrment = gtk.SCROLL_PAGES

BrentDC 2009-04-06 00:36

Re: Python: scroll a page with keypress; help needed.
 
Today I really tried to get this working...but to no avail. My code is as follows:

Code:

      if event.keyval == gtk.keysyms.Up:
        adj = self.sw.get_vadjustment()
        adj.page_increment = gtk.SCROLL_PAGES
        val = adj.get_value()
        adj.set_value(val+(gtk.SCROLL_PAGE))
        self.sw.set_vadjustment(adj)

If anyone has any other ideas, I'll try 'em out, but if not, I'll just not implement hardware key scrolling (I don't think it's a huge deal).

daperl 2009-04-06 01:17

Re: Python: scroll a page with keypress; help needed.
 
Quote:

Originally Posted by BrentDC (Post 277755)
Code:

      if event.keyval == gtk.keysyms.Up:
        adj = self.sw.get_vadjustment()
        adj.page_increment = gtk.SCROLL_PAGES
        val = adj.get_value()
        adj.set_value(val+(gtk.SCROLL_PAGE))
        self.sw.set_vadjustment(adj)


If you're paging up, shouldn't that be a minus sign? Also, aren't you missing an 'S'?

daperl 2009-04-06 01:46

Re: Python: scroll a page with keypress; help needed.
 
Also, you're not using the most important gtk.Adjustment attribute: 'page_size'.

And don't forget end conditions. Here's some code:

Code:

def pageup(self, sw):
    newval = sw.props.vadjustment.value - sw.props.vadjustment.page_size
    if newval < sw.props.vadjustment.lower: newval = sw.props.vadjustment.lower
    sw.props.vadjustment.value = newval

Just use the ScrolledWindow properties. pagedown in next post.

daperl 2009-04-06 01:55

Re: Python: scroll a page with keypress; help needed.
 
Code:

def pagedown(self, sw):
    newval = sw.props.vadjustment.value + sw.props.vadjustment.page_size
    max = sw.props.vadjustment.upper - sw.props.vadjustment.page_size
    if newval > max: newval = max
    sw.props.vadjustment.value = newval



All times are GMT. The time now is 17:10.

vBulletin® Version 3.8.8