Danny White
Danny White

Reputation: 103

Skip and fetch the elements from the list

I have got a problem with fetching the elements from the list. When I'm pressing on the Page down button of the keyboard, it will call the PageDown function as it will skip the 7 elements and it will fetch the 7 next elements from the list which it is:

['108 Sky Atlantic', '401 Sky Sports 1 UK', '402 SKY Sports 2 UK', '403 Sky Sports 3 UK', '404 Sky Sports 4 UK', '405 Sky Sports 5 UK', '406 Sky Sports News HQ']

When I hit on the down arrow button of the keyboard, it will skip the 7 elements but it will not fetch the 7 elements that come after 406 Sky Sports News HQ as it will fetch the elements which it will come after the 413 Eurosport 1 UK so the channels_list will show like this which it is incorrect:

['414 Eurosport 2 UK', '415 BT Sport 1', '416 BT Sport 2',
 '417 At the Races', '418 BT Sport ESPN', '419 MUTV', '420 Chelsea TV']

It should be:

['407 Sky Sports F1', '408 Sky Sports 1 HD', '409 Sky Sports 2 HD', '410 Sky Sports 3 HD', '411 Sky Sports 4 HD', '412 Sky Sports 5 HD', '413 Eurosport 1 UK']

Here is the code:

def PageDown(self):
    self.channel = [
        '101 BBC One S East', '102 BBC Two', '103 ITV', '104 Channel 4', 
        '105 Channel 5', '106 Sky One', '107 Sky Living', '108 Sky Atlantic', 
        '401 Sky Sports 1 UK', '402 SKY Sports 2 UK', '403 Sky Sports 3 UK', 
        '404 Sky Sports 4 UK', '405 Sky Sports 5 UK', '406 Sky Sports News HQ', 
        '407 Sky Sports F1', '408 Sky Sports 1 HD', '409 Sky Sports 2 HD', 
        '410 Sky Sports 3 HD', '411 Sky Sports 4 HD', '412 Sky Sports 5 HD', 
        '413 Eurosport 1 UK', '414 Eurosport 2 UK', '415 BT Sport 1', 
        '416 BT Sport 2', '417 At the Races', '418 BT Sport ESPN', '419 MUTV', 
        '420 Chelsea TV', '421 Eir Sport 1', '422 Eir Sport 2']

    channels_list = list()
    channels_Index += 7

    for i, start_ch in enumerate(self.channel):
        if start_ch == channel:
            channels_list = 
                self.channel[i+self.channels_Index:i+self.channels_Index+7]

What I am expecting to achieve is I want to skip the 7 elements and fetch the 7 elements in each time when I hit on the page down button of the keyboard.

Here is for example:

When I hit on the page down button, I want to skip the 7 elements and fetch the 7 next elements which show like this: ['108 Sky Atlantic', '401 Sky Sports 1 UK', '402 SKY Sports 2 UK', '403 Sky Sports 3 UK', '404 Sky Sports 4 UK', '405 Sky Sports 5 UK', '406 Sky Sports News HQ']

So when I hit on the page down button, I want to get the 7 next elements:

['407 Sky Sports F1', '408 Sky Sports 1 HD', '409 Sky Sports 2 HD', '410 Sky Sports 3 HD', '411 Sky Sports 4 HD', '412 Sky Sports 5 HD', '413 Eurosport 1 UK']

When I hit on the page down button again, I want to fetch the 7 next elements:

['414 Eurosport 2 UK', '415 BT Sport 1', '416 BT Sport 2', '417 At the Races', '418 BT Sport ESPN', '419 MUTV', '420 Chelsea TV']

And when finally I hit on the page down button again, I want to fetch the 7 elements that come near to the end of the list to make it to show like this:

['416 BT Sport 2', '417 At the Races', '418 BT Sport ESPN', '419 MUTV', '420 Chelsea TV', '421 Eir Sport 1', '422 Eir Sport 2']

Can you please show me an example how I can skip the 7 elements and fetch the 7 elements from the list in each time when I hit on the page down button of the keyboard?

Upvotes: 0

Views: 70

Answers (3)

ragardner
ragardner

Reputation: 1975

This demonstrates scrolling through 7 channels at a time, the key part being next_seven_channels = self.all_channels[self.channel_index:end_index]

try:
    import Tkinter as tk
except:
    import tkinter as tk

class app(tk.Tk):
    def __init__(self):
        tk.Tk.__init__(self)
        self.all_channels =  [
        '101 BBC One S East', '102 BBC Two', '103 ITV', '104 Channel 4', 
        '105 Channel 5', '106 Sky One', '107 Sky Living', '108 Sky Atlantic', 
        '401 Sky Sports 1 UK', '402 SKY Sports 2 UK', '403 Sky Sports 3 UK', 
        '404 Sky Sports 4 UK', '405 Sky Sports 5 UK', '406 Sky Sports News HQ', 
        '407 Sky Sports F1', '408 Sky Sports 1 HD', '409 Sky Sports 2 HD', 
        '410 Sky Sports 3 HD', '411 Sky Sports 4 HD', '412 Sky Sports 5 HD', 
        '413 Eurosport 1 UK', '414 Eurosport 2 UK', '415 BT Sport 1', 
        '416 BT Sport 2', '417 At the Races', '418 BT Sport ESPN', '419 MUTV', 
        '420 Chelsea TV', '421 Eir Sport 1', '422 Eir Sport 2']
        self.channel_index = 0

        self.print_channels = tk.Button(self,text="Page Down",
                                        font=("Calibri",30,"bold"),
                                        foreground="DodgerBlue2",
                                        command=self.PageDown)
        self.print_channels.pack()

    def PageDown(self,event=None):
        end_index = self.channel_index + 7
        if end_index > len(self.all_channels):
            end_index -= len(self.all_channels) - end_index   
        next_seven_channels = self.all_channels[self.channel_index:end_index]
        self.channel_index += 7
        if self.channel_index > len(self.all_channels):
            self.channel_index = 0
        print (next_seven_channels)

root = app()
root.mainloop()

If I understand correctly, to immediately show the first 7 channels, just call self.PageDown() after program starts, as below:

try:
    import Tkinter as tk
except:
    import tkinter as tk

class app(tk.Tk):
    def __init__(self):
        tk.Tk.__init__(self)
        self.all_channels =  [
        '101 BBC One S East', '102 BBC Two', '103 ITV', '104 Channel 4', 
        '105 Channel 5', '106 Sky One', '107 Sky Living', '108 Sky Atlantic', 
        '401 Sky Sports 1 UK', '402 SKY Sports 2 UK', '403 Sky Sports 3 UK', 
        '404 Sky Sports 4 UK', '405 Sky Sports 5 UK', '406 Sky Sports News HQ', 
        '407 Sky Sports F1', '408 Sky Sports 1 HD', '409 Sky Sports 2 HD', 
        '410 Sky Sports 3 HD', '411 Sky Sports 4 HD', '412 Sky Sports 5 HD', 
        '413 Eurosport 1 UK', '414 Eurosport 2 UK', '415 BT Sport 1', 
        '416 BT Sport 2', '417 At the Races', '418 BT Sport ESPN', '419 MUTV', 
        '420 Chelsea TV', '421 Eir Sport 1', '422 Eir Sport 2']
        self.channel_index = 0

        self.print_channels = tk.Button(self,text="Page Down",
                                        font=("Calibri",30,"bold"),
                                        foreground="DodgerBlue2",
                                        command=self.PageDown)
        self.print_channels.pack()
        self.PageDown() # added code <----

    def PageDown(self,event=None):
        end_index = self.channel_index + 7
        if end_index > len(self.all_channels):
            end_index -= len(self.all_channels) - end_index   
        next_seven_channels = self.all_channels[self.channel_index:end_index]
        self.channel_index += 7
        if self.channel_index > len(self.all_channels):
            self.channel_index = 0
        print (next_seven_channels)

root = app()
root.mainloop()

Or to skip showing the first 7 channels when the program starts:

try:
    import Tkinter as tk
except:
    import tkinter as tk

class app(tk.Tk):
    def __init__(self):
        tk.Tk.__init__(self)
        self.all_channels =  [
        '101 BBC One S East', '102 BBC Two', '103 ITV', '104 Channel 4', 
        '105 Channel 5', '106 Sky One', '107 Sky Living', '108 Sky Atlantic', 
        '401 Sky Sports 1 UK', '402 SKY Sports 2 UK', '403 Sky Sports 3 UK', 
        '404 Sky Sports 4 UK', '405 Sky Sports 5 UK', '406 Sky Sports News HQ', 
        '407 Sky Sports F1', '408 Sky Sports 1 HD', '409 Sky Sports 2 HD', 
        '410 Sky Sports 3 HD', '411 Sky Sports 4 HD', '412 Sky Sports 5 HD', 
        '413 Eurosport 1 UK', '414 Eurosport 2 UK', '415 BT Sport 1', 
        '416 BT Sport 2', '417 At the Races', '418 BT Sport ESPN', '419 MUTV', 
        '420 Chelsea TV', '421 Eir Sport 1', '422 Eir Sport 2']
        self.channel_index = 7 # changed code <----

        self.print_channels = tk.Button(self,text="Page Down",
                                        font=("Calibri",30,"bold"),
                                        foreground="DodgerBlue2",
                                        command=self.PageDown)
        self.print_channels.pack()

    def PageDown(self,event=None):
        end_index = self.channel_index + 7
        if end_index > len(self.all_channels):
            end_index -= len(self.all_channels) - end_index   
        next_seven_channels = self.all_channels[self.channel_index:end_index]
        self.channel_index += 7
        if self.channel_index > len(self.all_channels):
            self.channel_index = 0
        print (next_seven_channels)

root = app()
root.mainloop()

Upvotes: 1

sapht
sapht

Reputation: 2829

You change the value of channels_Index, which is a global variable that you haven't posted in your code (I assume; otherwise python will throw NameError), but you read the value of self.channels_Index. You probably meant self.channels_Index += 7

Upvotes: 1

theBrainyGeek
theBrainyGeek

Reputation: 584

Move the line

channels_Index += 7

It should work now. Put it after the loop so that it can be called again.

Upvotes: 1

Related Questions